ModelDriven 和 Preparable 拦截器

Params 拦截器

Parameters 拦截器将把表单字段映射到 ValueStack 栈的栈顶对象的各个属性中. 如果某个字段在模型里没有匹配的属性, Param 拦截器将尝试 ValueStack 栈中的下一个对象

在使用 Struts 作为前端的企业级应用程序时把 Action 和 Model 清晰地隔离开是有必要的: 有些 Action 类不代表任何Model 对象, 它们的功能仅限于提供显示服务

ModelDriven 拦截器

当用户触发 add 请求时, ModelDriven 拦截器将调用 EmployeeAction 对象的 getModel() 方法, 并把返回的模型(Employee实例)压入到 ValueStack 栈.
接下来 Parameters 拦截器将把表单字段映射到 ValueStack 栈的栈顶对象的各个属性中. 因为此时 ValueStack 栈的栈顶元素是刚被压入的模型(Employee)对象, 所以该模型将被填充. 如果某个字段在模型里没有匹配的属性, Param 拦截器将尝试 ValueStack 栈中的下一个对象

Preparable 拦截器

Struts 2.0 中的 modelDriven 拦截器负责把 Action 类以外的一个对象压入到值栈栈顶
而 prepare 拦截器负责准备为 getModel() 方法准备 model

PrepareInterceptor拦截器用方法

若 Action 实现 Preparable 接口,则 Action 方法需实现 prepare() 方法
PrepareInterceptor 拦截器将调用 prepare() 方法,prepareActionMethodName()方法 或 prepareDoActionMethodName ()方法 
PrepareInterceptor 拦截器根据 firstCallPrepareDo 属性决定获取 prepareActionMethodName 、prepareDoActionMethodName的顺序。默认情况下先获取 prepareActionMethodName (), 如果没有该方法,就寻找prepareDoActionMethodName()。如果找到对应的方法就调用该方法
PrepareInterceptor 拦截器会根据 alwaysInvokePrepare 属性决定是否执行prepare()方法

给出两个个实例:

① ModelDriven单个使用即可

[html] view plain copy
  1. import com.atguigu.struts2.model.Department;  
  2. import com.opensymphony.xwork2.ActionSupport;  
  3. import com.opensymphony.xwork2.ModelDriven;  
  4.   
  5. public class TestComplextPropertyAction extends ActionSupport   
  6.     implements ModelDriven<Department>{  
  7.   
  8.     /**  
  9.      *   
  10.      */  
  11.     private static final long serialVersionUID = 1L;  
  12.   
  13.     @Override  
  14.     public String execute() throws Exception {  
  15.         System.out.println(department);  
  16.         return SUCCESS;  
  17.     }  
  18.       
  19.     private Department department;  
  20.   
  21.     @Override  
  22.     public Department getModel() {  
  23.         department = new Department();  
  24.         return department;  
  25.     }  
  26.       
  27. }  


② ModelDriven和Preparable接口配合使用

[html] view plain copy
  1. package org.andy.shop.action;  
  2.   
  3. import org.andy.shop.dao.impl.UserInfoDaoImpl;  
  4. import org.andy.shop.entity.UserInfo;  
  5. import org.apache.log4j.Logger;  
  6. import org.springframework.beans.factory.annotation.Autowired;  
  7. import org.springframework.stereotype.Component;  
  8.   
  9. import com.opensymphony.xwork2.ModelDriven;  
  10. import com.opensymphony.xwork2.Preparable;  
  11. import com.sun.net.httpserver.Authenticator.Success;  
  12.   
  13. @Component  
  14. public class LoginAction implements ModelDriven<UserInfo>,Preparable{  
  15.   
  16.     private static final Logger LOGGER = Logger.getLogger(LoginAction.class);  
  17.     @Autowired  
  18.     private UserInfoDaoImpl userInfoDaoImpl;  
  19.     /**  
  20.      * 给action中的对象赋值:  
  21.      * 1)可以使用ModelDriven<T>配合Preparable来使用,下面给出了实例  
  22.      * 2)还可以在jsp中将input的name设置为userInfo.id来进行赋值  
  23.      */  
  24.     private UserInfo userInfo;  
  25.       
  26.     public void setUserInfo(UserInfo userInfo) {  
  27.         this.userInfo = userInfo;  
  28.     }  
  29.     public UserInfo getUserInfo() {  
  30.         return userInfo;  
  31.     }  
  32.   
  33.     public String saveUserInfo(){  
  34.         System.out.println(this.userInfo.toString());  
  35.         Integer userId = userInfoDaoImpl.save(userInfo);  
  36.         System.out.println(userId);  
  37.         return "success";  
  38.     }  
  39.       
  40.     /**  
  41.      * 为每一个方法定制一个prepar_MethodName,它会为saveUserInfo准备一个model对象,当ModelDriven调用getModel方法,将此对象压入栈顶中  
  42.     * @author 高国藩  
  43.     * @date 2015年11月4日 下午1:33:49  
  44.      */  
  45.     public void prepareSaveUserInfo(){  
  46.         this.userInfo = new UserInfo();  
  47.     }  
  48.       
  49.     public void prepare() throws Exception {  
  50.         System.out.println("prepare ...");  
  51.     }  
  52.     /**  
  53.      * 为ModelDriven返回一个实例  
  54.      */  
  55.     public UserInfo getModel() {  
  56.         return userInfo;  
  57.     }  
  58. }  

在上面的类中,strust会首先执行prepareSaveUserInfo()这样一个方法,为其方法saveUserInfo()准备一个对象,在getModel()的时候用于返回,但是,strust每次都会执行prepare()这个方法,如果想要去掉该方法的执行,需要实现如下的xml配置(修改alwaysInvokePrepare 常量的值即可):

[html] view plain copy
  1.   <package name="default" namespace="/" extends="struts-default">  
  2.   
  3. <!-- 配置使用 paramsPrepareParamsStack 作为默认的拦截器栈 -->  
  4. <!-- 修改 PrepareInterceptor 拦截器的 alwaysInvokePrepare 属性值为 false -->  
  5. <interceptors>  
  6.     <interceptor-stack name="atguigustack">  
  7.         <interceptor-ref name="paramsPrepareParamsStack">  
  8.             <param name="prepare.alwaysInvokePrepare">false</param>  
  9.         </interceptor-ref>  
  10.     </interceptor-stack>  
  11. </interceptors>  
  12.   
  13. <default-interceptor-ref name="atguigustack"/>  
  14.   
  15. <action name="emp-*"   
  16.     class="com.atguigu.struts2.app.EmployeeAction"  
  17.     method="{1}">  
  18.     <result name="{1}">/emp-{1}.jsp</result>  
  19.     <result name="success" type="redirectAction">emp-list</result>    
  20. </action>  
  21.   
  22. <action name="emp-list"  
  23.     class="com.atguigu.struts2.app.EmployeeAction"  
  24.     method="list">  
  25.     <result name="list">/emp-list.jsp</result>    
  26. </action>  
  27.       
  28.   </package>  


通常我们本不是这样使用的,在页面中,我们通常会这样写这么一个form

[html] view plain copy
  1. <s:textfield name="project.id"></s:textfield>  
  2. <s:textfield name="project.name"></s:textfield>  
  3. <s:textfield name="project.age"></s:textfield>  

就不需要使用上面讲述的两个借口了,更多的情况,我们会传递多个对象过去,那么页面会这样写:

[html] view plain copy
  1. private Collection<Person> persons;  
  2.   
  3.    public String savePs(){  
  4.     for (Person person : persons) {  
  5.         System.out.println(person.toString());  
  6.     }  
  7.        return "hello_world";  
  8.    }  
  9.   
  10. public Collection<Person> getPersons() {  
  11.     return persons;  
  12. }  
  13.   
  14. public void setPersons(Collection<Person> persons) {  
  15.     this.persons = persons;  
  16. }  

而在页面上我们这样写:

[html] view plain copy
  1. <form action="hello-world-default/HelloWordAction_savePs.action"  
  2.     method="post">  
  3.     person.id:<input type="text" name="persons[0].id"><br>  
  4.     person.name:<input type="text" name="persons[0].name"><br>  
  5.     <br> person.id:<input type="text" name="persons[1].id"><br>  
  6.     person.name:<input type="text" name="persons[1].name"><br>  
  7.     <input type="submit" value="提交">  
  8. </form>  

这样子写好以后呢,当使用下一篇中的类型转换,不管是List还是Collection等,都能轻松转换,无需写额外的代码了...

原文地址:https://www.cnblogs.com/zhanghengscnc/p/8503545.html