当前位置: 移动技术网 > IT编程>开发语言>Java > struts2的流程和一系列相关知识代码解析

struts2的流程和一系列相关知识代码解析

2019年07月19日  | 移动技术网IT编程  | 我要评论

1.客户端初始化一个指向servlet容器(tomcat)的请求;

2.这个请求经过一系列的过滤器,接着filterdispatcher被调用;

3.filterdispatcher询问actionmapper来决定这个请求是否要调用某个action;

4.如果actionmapper决定调用某个action,filterdispatcher把请求的处理交给actionproxy,actionpro根据configurationmanager询问框架的配置文件,找到需要调用的action类,一般都是读取struts.xml;

5.actionproxy创建一个actioninvocation的实例,actioninvocation实例使用命名模式来调用,在调用action的过程前后,涉及到相关拦截器的调用;

6.一旦action执行完毕,actioninvocation根据struts.xml中的配置找到对应的返回结果

比如代码:

struts2获得了.action请求后,将根据部分决定调用哪个业务逻辑组件;

struts2应用中的action都被定义在struts.xml中;

struts2用于处理用户请求的action实例并不是用户实现的业务控制器,而是action代理,因为用户实现的业务控制器并没有与servletapi耦合,显然无法处理用户的请求。

<html>
 <head>
  <title>success</title>
 </head>
 <body>
  <form action="hello.action" method="post">
    username:<input type="text" name="name"></br>
    password:<input type="password" name="pass"></br>
    <input type="submit" value="提交">
  </form>
 </body>
</html>

比如上面表单的hello.action,这个action属性不是一个普通的servlet,也不是一个动态jsp页面,当表单提交给hello.action时,struts2的filterdispatcher将会起作用,将用户请求转发到对应的action.

注意的是:struts2 action默认拦截所有后缀为.action的请求,如果我们需要将表单提交给action处理,则应将表单action属性设置为.action的格式。

控制器类

public class helloaction {
	private string name;
	private string pass;
	public void setname(string name){
		this.name=name;
	}
	public void setpass(string pass){
		this.pass=pass;
	}
	public string execute(){
		if("yang".equals(name) && "1234".equals(pass)){
			return "success";
		} else{
			return "error";
		}
	}
}

前面执行完成后仅仅是执行了页面的转发,没有跟踪用户的状态,当用户登录完成后,我们需要将用户的用户名添加为httpsession的状态信息。

为了访问httpsession实例,struts2提供了一个actioncontext类,该类提供了一个getsession()得方法,但是这个方法的返回值不是httpsession()而是map(),但是struts2的拦截器会负责该session()和httpsession()之间的切换。

为了检查我们设置的session属性是否成功,可以给成功后的界面这么设置

<html>
 <head>
  <base href="<%=basepath%>" rel="external nofollow" >
  <title>success</title>
 </head>
 <body>
  欢迎,${sessionscope.user},您已经登录。
 </body>
</html>

利用jsp2.0表达式语法输出http session中的user属性。

action 工具类集成actionsupport

actionsupport类是一个工具类,而且已经实现了action接口,除此之外,还实现了validateablez接口,提供了数据校验功能。

为了增加输入数据的校验功能,在action中增加重写validate方法。

public void validate() {
	if(getname()==null || getname().trim().equals("")){
		addfielderror("name",gettext("name.required"));
	}
	if(getpass()==null || getpass().trim().equals("")){
		addfielderror("pass", gettext("pass.required"));
	}
}

上面添加的重写的validate方法会在系统的execute()方法之前执行,如果执行该方法后action类的fielderror中已经包含了数据校验错误,请求将被转发到input逻辑视图处,所以还要在struts.xml中添加input逻辑视图名,让其跳转到登录页面。

这个validate方法的缺点就是需要大量重写validate方法,所以可以用struts2的校验框架进行校验。

<?xml version="1.0" encoding="utf-8"?>
<!doctype validators public
  "-//opensymphony group//xwork validator 1.0.3//en"
  "http://www.opensymphony.com/xwork/xwork-validator-1.0.3.dtd">
<validators>
<!--验证表单name-->
  <field name="name">
    <field-validator type="requiredstring">
      <message key="name.required"/>
    </field-validator>
  </field>
  <!--验证表单pass-->
  <field name="pass">
    <field-validator type="requiredstring">
      <message key="pass.required"/>
    </field-validator>
  </field>
</validators>

总结

以上就是本文关于struts2的流程和一系列相关知识代码解析的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站其他相关专题,如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网