Struts2中Action对象的set方法和get方法调用规则
Struts的Action是采用的是多实例多线程设计,而不是像Servlet那样采用单实例多线程设计,因此在struts中,一个请求就对应一个Action对象,个对象之间的数据相互之间互不干扰。没接到一个新的请求,就产生一个新的Action对象,并对Action对象的各属性赋予默认初始值。之后再根据一定的规则调用set方法给各属性赋值,并根据一定规则调用get方法。也就是说,Action对象不是一开始就必须调用set方法的。本文重点讲Action对象调用set和get方法的规则:
1.get方法的调用规则
Action对象get方法的调用规则相对简单,因此先说get方法的调用规则。调用get方法方法是为了获取属性值,这一点不仅仅在struts中是这样,在任何java程序中,都是这样的。那么什么时候才需要一个Action对象的属性值呢?就目前本人探索和总结的结果来看,分为以下三种情况:
(一)、在Action请求转发到下一个Action时(即result的type属性为chain时),下一个Action有对应属性的set方法;
这种情况是为了传递数据,让数据跟着Action流程的流转而进入到下一个Action对象,因此是非常重要的。但是呢,这种传递不一定能达到想要的目的,意外出现在如果最初的http请求本身就有了对应的查询参数值,则目标Action会在获得了源Action的get值之后,再次去过的请求的查询参数值,并用这个值作为set方法的传入参数,在这种情况下,目标Filed的值还是http请求的查询参数值。
(二)、在struts.xml配置文件中引用了相应的Action属性;
这种情况其实是显式调用了get方法,所以一定会被调用,也没什么好说的。
(三)有对应的request.getAttribute(),且之前没有用过相应的request.setAttribute()。
这里说的request.getAttribute()一般出现在视图中,比如jsp的requestScope.filedName,或者显示的request.getAttribute() java 代码。
Action对象get方法调用的具体规则流程如下:
需要强调的是,只有在真正需要数据的时候,才会调用get方法,而不是在离开Action的时候。所以,在下一个Action的拦截器之后,到达Action时,上一个Action的get方法才被调用(因为这时需要给目标Action赋值了,而赋值需要值的来源,所以就要调用上一个Action的get方法以获得赋值的来源)。
还有需要强调的是,需要get方法时,总是调用最近Action对象的get方法,如果最近的Action没有get方法,则调用次近的,知道最开始的那个Action。
2.set方法的调用规则
Action对象set方法的调用规则相对于get方法来说比较复杂。同样,在任何java程序中,set方法都是为了给对象的属性赋值,因此,set方法的调用规则关乎到Action对象的数据是怎么获取的,因此显得尤为重要。
因为set方法是给Action对象数据赋值的,所以,要知道set方法的调用规则,首先要清楚Action对象的属性数据的来源有哪些。就本人目前探索和总结来看,Action对象的属性数据的来源有以下两类:(1)上一个Action的get方法、(2)http请求参数。这两类数据来源也就对应了set方法的调用规则。
(一)从源Action的get方法中获得
这种情况对应于get方法调用规则中的第一种情况,也是在Action转发时发生的,而且和get方法的第一种情况总是同时发生的,也就是说,这两张情况要么都发生,要么都不发生。两种情况共同完成Action对象的数据传递。
(二)从请求参数中来
如果有对应的请求参数值,则这种情况一定会发生,及时上一种情况已经发生过了。这样,如果源Action有对应的get方法,请求中也有对应的请求参数值(注意是getParameter到的值,而不是getAttribute到的值,Action是不能从request的Attribute中得到属性值的),则一个get方法会被调用两次,后面一次获取的是请求参数中的值,最终的属性值和请求参数一致。
set方法调用规则的流程如下:
测试用的代码如下:LoginAction是源Action,LoginAction2是目标Action。
1 import com.opensymphony.xwork2.ActionSupport; 2 import org.apache.logging.log4j.LogManager; 3 import org.apache.logging.log4j.Logger; 4 import org.apache.struts2.ServletActionContext; 5 6 import javax.servlet.http.HttpServletRequest; 7 import javax.servlet.http.HttpServletResponse; 8 9 public class LoginAction2 extends ActionSupport{ 10 private final Logger log = LogManager.getLogger(getClass()); 11 private String username; 12 private String password; 13 private String usernameAndPassword; 14 private int age; 15 16 public String getUsername() { 17 log.trace("getUsername is in invoked"); 18 19 return username; 20 } 21 22 public void setUsername(String username) { 23 log.trace("setUsername is in invoked"); 24 25 this.username = username; 26 } 27 28 public String getPassword() { 29 return password; 30 } 31 32 public void setPassword(String password) { 33 this.password = password; 34 } 35 36 public String getUsernameAndPassword() { 37 log.trace("getUsernameAndPassword is in invoked"); 38 39 return usernameAndPassword; 40 } 41 42 public void setUsernameAndPassword(String usernameAndPassword) { 43 log.trace("setUsernameAndPassword is in invoked"); 44 45 this.usernameAndPassword = usernameAndPassword; 46 } 47 48 @Override 49 public void validate() { 50 log.trace("validate is invoked"); 51 HttpServletRequest request = ServletActionContext.getRequest(); 52 HttpServletResponse response = ServletActionContext.getResponse(); 53 System.out.println( request.getAttribute("age")); 54 } 55 56 public int getAge() { 57 log.trace("getAge is invoked"); 58 System.out.println("getAge: "+age); 59 60 return 70; 61 } 62 63 // public void setAge(int age) { 64 // log.trace("setAge is invoked"); 65 // System.out.println("setAge: "+age); 66 // this.age = age; 67 // } 68 }
1 package edu.whu.swe.learnstruts2; 2 3 import com.opensymphony.xwork2.ActionContext; 4 import com.opensymphony.xwork2.ActionSupport; 5 import edu.whu.swe.learnstruts2.service.LoginService; 6 import edu.whu.swe.learnstruts2.service.impl.LoginServiceImpl; 7 import org.apache.logging.log4j.LogManager; 8 import org.apache.logging.log4j.Logger; 9 import org.apache.struts2.ServletActionContext; 10 11 import javax.servlet.RequestDispatcher; 12 import javax.servlet.ServletContext; 13 import javax.servlet.http.HttpServletRequest; 14 import javax.servlet.http.HttpServletResponse; 15 16 17 public class LoginAction extends ActionSupport { 18 private final Logger log = LogManager.getLogger(getClass()); 19 private String username; 20 private String password; 21 private String alianname; 22 private String othertest; 23 private int age; 24 25 private final LoginService loginService = new LoginServiceImpl(); 26 27 28 public String execute() throws Exception { 29 30 31 if (!loginService.isLogin(username, password)) { 32 33 return INPUT; 34 35 } 36 HttpServletRequest request = ServletActionContext.getRequest(); 37 HttpServletResponse response = ServletActionContext.getResponse(); 38 // request.setAttribute("age", 22); 39 // this.age=22; 40 41 /** 因为是使用的框架,所以重定向语句之后的代码依旧会执行,但是业务逻辑已经转到定向后的地方了 */ 42 // ServletContext context =request.getSession().getServletContext(); 43 // RequestDispatcher dispatcher=context.getRequestDispatcher("/usernameInvalide.jsp"); 44 // System.out.println("print this before dispatcher"); 45 // dispatcher.forward(request,response); 46 // System.out.println("print this after dispatcher"); 47 // age+=1000; 48 49 50 // 51 System.out.println("LoginAction is executed"); 52 return SUCCESS; 53 54 } 55 56 public String getUsernameAndPassword() { 57 log.trace("getUsernameAndPassword is in invoked"); 58 return username + password; 59 } 60 61 public void setUsernameAndPassword(String usernameAndPassword) { 62 log.trace("setUsernameAndPassword is in invoked"); 63 64 } 65 66 67 public String getUsername() { 68 log.trace("getUsername is in invoked"); 69 return username; 70 } 71 72 public void setUsername(String username) { 73 log.trace("setUsername is in invoked"); 74 this.username = username; 75 } 76 77 public String getPassword() { 78 return password; 79 } 80 81 public void setPassword(String password) { 82 this.password = password; 83 } 84 85 86 public String getAlianame() { 87 log.trace("getAlianame is in invoked"); 88 89 return alianname; 90 } 91 92 public void setAlianame(String alianname) { 93 log.trace("setAlianame is in invoked"); 94 95 this.alianname = alianname; 96 } 97 98 public String getOthertest() { 99 return othertest; 100 } 101 102 public void setOthertest(String othertest) { 103 this.othertest = othertest; 104 } 105 106 107 @Override 108 public void validate() { 109 log.trace("validate is invoked"); 110 } 111 112 public void setAge(int age) { 113 log.trace("setAge is invoked"); 114 this.age = age + 33; 115 } 116 117 public int getAge() { 118 log.trace("setAge is invoked"); 119 return 55; 120 } 121 }
关键的struts.xml配置如下:
<action name="login1" class="edu.whu.swe.learnstruts2.LoginAction2"> <result name="success">/welcome.jsp</result> </action> <action name="login" class="edu.whu.swe.learnstruts2.LoginAction"> <result name="success" type="chain"> <param name="actionName">login1</param> <param name="namespace">/</param> </result> </action>
测试结果如下:
不输入age 2018-01-12 16:34:58.876 TRACE edu.whu.swe.learnstruts2.LoginAction setUsername - " setUsername is in invoked " 2018-01-12 16:34:58.877 TRACE edu.whu.swe.learnstruts2.LoginAction validate - " validate is invoked " LoginAction is executed 2018-01-12 16:34:58.877 TRACE edu.whu.swe.learnstruts2.LoginAction getAge - " setAge is invoked " 2018-01-12 16:34:58.878 TRACE edu.whu.swe.learnstruts2.LoginAction2 setAge - " setAge is invoked " 2018-01-12 16:34:58.878 TRACE edu.whu.swe.learnstruts2.LoginAction getUsername - " getUsername is in invoked " 2018-01-12 16:34:58.878 TRACE edu.whu.swe.learnstruts2.LoginAction2 setUsername - " setUsername is in invoked " 2018-01-12 16:34:58.878 TRACE edu.whu.swe.learnstruts2.LoginAction getUsernameAndPassword - " getUsernameAndPassword is in invoked " 2018-01-12 16:34:58.879 TRACE edu.whu.swe.learnstruts2.LoginAction2 setUsernameAndPassword - " setUsernameAndPassword is in invoked " 2018-01-12 16:34:58.879 TRACE edu.whu.swe.learnstruts2.LoginAction2 setUsername - " setUsername is in invoked " 2018-01-12 16:34:58.880 TRACE edu.whu.swe.learnstruts2.LoginAction2 validate - " validate is invoked " 2018-01-12 16:34:58.883 TRACE edu.whu.swe.learnstruts2.LoginAction2 getUsername - " getUsername is in invoked " 2018-01-12 16:34:58.883 TRACE edu.whu.swe.learnstruts2.LoginAction2 getAge - " getAge is invoked " 2018-01-12 16:34:58.884 TRACE edu.whu.swe.learnstruts2.LoginAction2 getUsernameAndPassword - " getUsernameAndPassword is in invoked " 输入了age 2018-01-12 16:35:36.356 TRACE edu.whu.swe.learnstruts2.LoginAction setAge - " setAge is invoked " 2018-01-12 16:35:36.356 TRACE edu.whu.swe.learnstruts2.LoginAction setUsername - " setUsername is in invoked " 2018-01-12 16:35:36.357 TRACE edu.whu.swe.learnstruts2.LoginAction validate - " validate is invoked " LoginAction is executed 2018-01-12 16:35:36.358 TRACE edu.whu.swe.learnstruts2.LoginAction getAge - " setAge is invoked " 2018-01-12 16:35:36.358 TRACE edu.whu.swe.learnstruts2.LoginAction2 setAge - " setAge is invoked " 2018-01-12 16:35:36.358 TRACE edu.whu.swe.learnstruts2.LoginAction getUsername - " getUsername is in invoked " 2018-01-12 16:35:36.359 TRACE edu.whu.swe.learnstruts2.LoginAction2 setUsername - " setUsername is in invoked " 2018-01-12 16:35:36.359 TRACE edu.whu.swe.learnstruts2.LoginAction getUsernameAndPassword - " getUsernameAndPassword is in invoked " 2018-01-12 16:35:36.359 TRACE edu.whu.swe.learnstruts2.LoginAction2 setUsernameAndPassword - " setUsernameAndPassword is in invoked " 2018-01-12 16:35:36.360 TRACE edu.whu.swe.learnstruts2.LoginAction2 setAge - " setAge is invoked " 2018-01-12 16:35:36.360 TRACE edu.whu.swe.learnstruts2.LoginAction2 setUsername - " setUsername is in invoked " 2018-01-12 16:35:36.360 TRACE edu.whu.swe.learnstruts2.LoginAction2 validate - " validate is invoked " 2018-01-12 16:35:36.363 TRACE edu.whu.swe.learnstruts2.LoginAction2 getUsername - " getUsername is in invoked " 2018-01-12 16:35:36.363 TRACE edu.whu.swe.learnstruts2.LoginAction2 getAge - " getAge is invoked " 2018-01-12 16:35:36.363 TRACE edu.whu.swe.learnstruts2.LoginAction2 getUsernameAndPassword - " getUsernameAndPassword is in invoked " 没有age表单 2018-01-12 16:37:41.549 TRACE edu.whu.swe.learnstruts2.LoginAction setUsername - " setUsername is in invoked " 2018-01-12 16:37:41.591 TRACE edu.whu.swe.learnstruts2.LoginAction validate - " validate is invoked " LoginAction is executed 2018-01-12 16:37:41.610 TRACE edu.whu.swe.learnstruts2.LoginAction getAge - " setAge is invoked " 2018-01-12 16:37:41.611 TRACE edu.whu.swe.learnstruts2.LoginAction2 setAge - " setAge is invoked " 2018-01-12 16:37:41.611 TRACE edu.whu.swe.learnstruts2.LoginAction getUsername - " getUsername is in invoked " 2018-01-12 16:37:41.611 TRACE edu.whu.swe.learnstruts2.LoginAction2 setUsername - " setUsername is in invoked " 2018-01-12 16:37:41.612 TRACE edu.whu.swe.learnstruts2.LoginAction getUsernameAndPassword - " getUsernameAndPassword is in invoked " 2018-01-12 16:37:41.612 TRACE edu.whu.swe.learnstruts2.LoginAction2 setUsernameAndPassword - " setUsernameAndPassword is in invoked " 2018-01-12 16:37:41.615 TRACE edu.whu.swe.learnstruts2.LoginAction2 setUsername - " setUsername is in invoked " 2018-01-12 16:37:41.622 TRACE edu.whu.swe.learnstruts2.LoginAction2 validate - " validate is invoked " 2018-01-12 16:37:41.741 TRACE edu.whu.swe.learnstruts2.LoginAction2 getUsername - " getUsername is in invoked " 2018-01-12 16:37:41.741 TRACE edu.whu.swe.learnstruts2.LoginAction2 getAge - " getAge is invoked " 2018-01-12 16:37:41.742 TRACE edu.whu.swe.learnstruts2.LoginAction2 getUsernameAndPassword - " getUsernameAndPassword is in invoked " 没有age表单,在源Action有getAge{return age+55}和setAge{this.age=age+33},返回页面结果是:age:55 2018-01-12 16:47:33.884 TRACE edu.whu.swe.learnstruts2.LoginAction setUsername - " setUsername is in invoked " 2018-01-12 16:47:33.933 TRACE edu.whu.swe.learnstruts2.LoginAction validate - " validate is invoked " LoginAction is executed 2018-01-12 16:47:33.955 TRACE edu.whu.swe.learnstruts2.LoginAction getAge - " setAge is invoked " 2018-01-12 16:47:33.955 TRACE edu.whu.swe.learnstruts2.LoginAction2 setAge - " setAge is invoked " 2018-01-12 16:47:33.956 TRACE edu.whu.swe.learnstruts2.LoginAction getUsername - " getUsername is in invoked " 2018-01-12 16:47:33.956 TRACE edu.whu.swe.learnstruts2.LoginAction2 setUsername - " setUsername is in invoked " 2018-01-12 16:47:33.956 TRACE edu.whu.swe.learnstruts2.LoginAction getUsernameAndPassword - " getUsernameAndPassword is in invoked " 2018-01-12 16:47:33.957 TRACE edu.whu.swe.learnstruts2.LoginAction2 setUsernameAndPassword - " setUsernameAndPassword is in invoked " 2018-01-12 16:47:33.960 TRACE edu.whu.swe.learnstruts2.LoginAction2 setUsername - " setUsername is in invoked " 2018-01-12 16:47:33.971 TRACE edu.whu.swe.learnstruts2.LoginAction2 validate - " validate is invoked " 2018-01-12 16:47:34.132 TRACE edu.whu.swe.learnstruts2.LoginAction2 getUsername - " getUsername is in invoked " 2018-01-12 16:47:34.133 TRACE edu.whu.swe.learnstruts2.LoginAction2 getAge - " getAge is invoked " 2018-01-12 16:47:34.134 TRACE edu.whu.swe.learnstruts2.LoginAction2 getUsernameAndPassword - " getUsernameAndPassword is in invoked " 输入age表单值为666,在源Action有getAge{return age+55}和setAge{this.age=age+33},在目标Action中打印相应的Filed。返回页面结果是:age:22 2018-01-12 17:19:51.730 TRACE edu.whu.swe.learnstruts2.LoginAction setAge - " setAge is invoked " 2018-01-12 17:19:51.731 TRACE edu.whu.swe.learnstruts2.LoginAction setUsername - " setUsername is in invoked " 2018-01-12 17:19:51.731 TRACE edu.whu.swe.learnstruts2.LoginAction validate - " validate is invoked " LoginAction is executed 2018-01-12 17:19:51.734 TRACE edu.whu.swe.learnstruts2.LoginAction getAge - " setAge is invoked " 2018-01-12 17:19:51.734 TRACE edu.whu.swe.learnstruts2.LoginAction2 setAge - " setAge is invoked " setAge: 754 2018-01-12 17:19:51.735 TRACE edu.whu.swe.learnstruts2.LoginAction getUsername - " getUsername is in invoked " 2018-01-12 17:19:51.736 TRACE edu.whu.swe.learnstruts2.LoginAction2 setUsername - " setUsername is in invoked " 2018-01-12 17:19:51.736 TRACE edu.whu.swe.learnstruts2.LoginAction getUsernameAndPassword - " getUsernameAndPassword is in invoked " 2018-01-12 17:19:51.736 TRACE edu.whu.swe.learnstruts2.LoginAction2 setUsernameAndPassword - " setUsernameAndPassword is in invoked " 2018-01-12 17:19:51.737 TRACE edu.whu.swe.learnstruts2.LoginAction2 setAge - " setAge is invoked " setAge: 666 2018-01-12 17:19:51.737 TRACE edu.whu.swe.learnstruts2.LoginAction2 setUsername - " setUsername is in invoked " 2018-01-12 17:19:51.738 TRACE edu.whu.swe.learnstruts2.LoginAction2 validate - " validate is invoked " 2018-01-12 17:19:51.741 TRACE edu.whu.swe.learnstruts2.LoginAction2 getUsername - " getUsername is in invoked " 2018-01-12 17:19:51.741 TRACE edu.whu.swe.learnstruts2.LoginAction2 getAge - " getAge is invoked " getAge: 666 2018-01-12 17:19:51.742 TRACE edu.whu.swe.learnstruts2.LoginAction2 getUsernameAndPassword - " getUsernameAndPassword is in invoked " 输入age表单值为666,在源Action有getAge{return age+55}和setAge{this.age=age+33},设置setAttribute("age",22);在目标Action中打印相应的Filed。返回页面结果是:age:22 2018-01-12 16:56:50.179 TRACE edu.whu.swe.learnstruts2.LoginAction setAge - " setAge is invoked " 2018-01-12 16:56:50.195 TRACE edu.whu.swe.learnstruts2.LoginAction setUsername - " setUsername is in invoked " 2018-01-12 16:56:50.229 TRACE edu.whu.swe.learnstruts2.LoginAction validate - " validate is invoked " LoginAction is executed 2018-01-12 16:56:50.249 TRACE edu.whu.swe.learnstruts2.LoginAction getAge - " setAge is invoked " 2018-01-12 16:56:50.250 TRACE edu.whu.swe.learnstruts2.LoginAction2 setAge - " setAge is invoked " setAge: 754 2018-01-12 16:56:50.250 TRACE edu.whu.swe.learnstruts2.LoginAction getUsername - " getUsername is in invoked " 2018-01-12 16:56:50.250 TRACE edu.whu.swe.learnstruts2.LoginAction2 setUsername - " setUsername is in invoked " 2018-01-12 16:56:50.251 TRACE edu.whu.swe.learnstruts2.LoginAction getUsernameAndPassword - " getUsernameAndPassword is in invoked " 2018-01-12 16:56:50.251 TRACE edu.whu.swe.learnstruts2.LoginAction2 setUsernameAndPassword - " setUsernameAndPassword is in invoked " 2018-01-12 16:56:50.253 TRACE edu.whu.swe.learnstruts2.LoginAction2 setAge - " setAge is invoked " setAge: 666 2018-01-12 16:56:50.255 TRACE edu.whu.swe.learnstruts2.LoginAction2 setUsername - " setUsername is in invoked " 2018-01-12 16:56:50.263 TRACE edu.whu.swe.learnstruts2.LoginAction2 validate - " validate is invoked " 2018-01-12 16:56:50.414 TRACE edu.whu.swe.learnstruts2.LoginAction2 getUsername - " getUsername is in invoked " 2018-01-12 16:56:50.415 TRACE edu.whu.swe.learnstruts2.LoginAction2 getUsernameAndPassword - " getUsernameAndPassword is in invoked "