Struts2之ModelDriven和Preparable拦截器
首先struts.xml文件配置如下 默认拦截器设置为paramsPrepareParamsStack
<package name="default" namespace="/" extends="struts-default"> <!-- 配置使用 paramsPrepareParamsStack 作为默认的拦截器栈 --> <!-- 修改 PrepareInterceptor 拦截器的 alwaysInvokePrepare 属性值为 false --> <interceptors> <interceptor-stack name="teststack"> <interceptor-ref name="paramsPrepareParamsStack"> <param name="prepare.alwaysInvokePrepare">false</param> </interceptor-ref> </interceptor-stack> </interceptors> <default-interceptor-ref name="teststack"/> <action name="emp-*" class="com.app.EmployeeAction" method="{1}"> <result name="{1}">/emp-{1}.jsp</result> <result name="success" type="redirectAction">emp-list</result> </action> <action name="emp-edit" <!--这段代码按理上面的应该可以用但是如果不加页面跳转错误 不清除是字打错了还是怎么了--> class="com.app.EmployeeAction" method="edit"> <result name="edit">/emp-edit.jsp</result> </action> </package>
拦截顺序
<interceptor-stack name="paramsPrepareParamsStack"> <interceptor-ref name="exception"/> <interceptor-ref name="alias"/> <interceptor-ref name="i18n"/> <interceptor-ref name="checkbox"/> <interceptor-ref name="datetime"/> <interceptor-ref name="multiselect"/> <interceptor-ref name="params"/> //1.栈顶对象为当前Action将参数赋值给Action类中对应属性 <interceptor-ref name="servletConfig"/> <interceptor-ref name="prepare"/> //2.调用prepareXxx方法为ModelDriven准备model <interceptor-ref name="chain"/> <interceptor-ref name="modelDriven"/> //3.将model中的参数置于栈顶 <interceptor-ref name="fileUpload"/> <interceptor-ref name="staticParams"/> <interceptor-ref name="actionMappingParams"/> <interceptor-ref name="params"/> //4.输入参数赋值给栈顶元素 <interceptor-ref name="conversionError"/> <interceptor-ref name="validation"> <param name="excludeMethods">input,back,cancel,browse</param> </interceptor-ref> <interceptor-ref name="workflow"> <param name="excludeMethods">input,back,cancel,browse</param> </interceptor-ref> </interceptor-stack>
Java代码 实现ModelDriven, Preparable接口
package com.app; import java.util.Map; import org.apache.struts2.interceptor.RequestAware; import com.opensymphony.xwork2.ModelDriven; import com.opensymphony.xwork2.Preparable; public class EmployeeAction implements RequestAware, ModelDriven<Employee>, Preparable{ private Dao dao = new Dao(); private Employee employee; public String update(){ dao.update(employee); return "success"; } public void prepareUpdate(){ employee = new Employee(); } public String edit(){ // System.out.println(222222); return "edit"; } public void prepareEdit(){ //System.out.println(1111111); employee = dao.get(employeeId); } public String save(){ dao.save(employee); return "success"; } public void prepareSave(){ employee = new Employee(); } public String delete(){ dao.delete(employeeId); return "success"; } public String list(){ request.put("emps", dao.getEmployees()); return "list"; } private Map<String, Object> request; @Override public void setRequest(Map<String, Object> arg0) { this.request = arg0; } private Integer employeeId; public void setEmployeeId(Integer employeeId) { this.employeeId = employeeId; } @Override public Employee getModel() { return employee; //将employee置于栈顶 } @Override public void prepare() throws Exception { System.out.println("prepare..."); } }
list.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <s:form action="emp-save"> <s:textfield name="name" label="Name"></s:textfield> <s:textfield name="email" label="Email"></s:textfield> <s:submit></s:submit> </s:form> <br> <hr> <br> <table cellpadding="10" cellspacing="0" border="1"> <thead> <tr> <td>ID</td> <td>Name</td> <td>Email</td> <td>Edit</td> <td>Delete</td> </tr> </thead> <tbody> <s:iterator value="#request.emps"> <tr> <td>${employeeId }</td> <td>${name }</td> <td>${email }</td> <td><a href="emp-edit?employeeId=${employeeId }">Edit</a></td> <td><a href="emp-delete?employeeId=${employeeId }">Delete</a></td> </tr> </s:iterator> </tbody> </table> </body> </html>
edit.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <s:debug></s:debug> <br> <br> <s:form action="emp-update"> <s:hidden name="employeeId"></s:hidden> <s:textfield name="name" label="Name"></s:textfield> <s:textfield name="email" label="Email"></s:textfield> <s:submit></s:submit> </s:form> </body> </html>