一、拦截器简介
Struts拦截器和Action的关系如图:
为了在使用拦截器时制定参数值,应通过<interceptor-ref …/>元素添加<param …/>子元素来为拦截器指定参数值。下面是配置拦截器动态指定参数值的语法。
1: <interceptor-stack name="拦截器栈一">2: <interceptor-ref name="拦截器一">3: <param name="参数一">参数值一</param>4: <param name="参数二">参数值二</param>5: </interceptor-ref>6: </interceptor-stack>
二、自定义拦截器
1、简单的拦截器
用户要开发自己的拦截器,需要实现com.opensymphony.xwork2.interceptor.Interceptor接口。下面实现了一个简单的拦截器
1、SimpleInterceptor.java
1: package cc.openhome;2: import java.util.Date;3: import com.opensymphony.xwork2.ActionInvocation;4: import com.opensymphony.xwork2.interceptor.AbstractInterceptor;5: public class SimpleInterceptor extends AbstractInterceptor {6: //简单的拦截器的名字7: private String name;8: //为该简单拦截器设置名字的setter方法9: //拦截Action方法的intercept方法10: @Override11: public String intercept(ActionInvocation invocation) throws Exception {12: // TODO Auto-generated method stub13: //取得被拦截的Action实例14: LoginAction action = (LoginAction) invocation.getAction();15: //打印16: System.out.println("正在执行 ‘"+name+"’ 拦截器的动作========"+"开始执行登录的时间为:"+new Date());17: //取得开始执行Action的时间18: long start = System.currentTimeMillis();19: //执行该拦截器的后一个拦截器,或者直接指定Action的execute()方法20: String result = invocation.invoke();21: System.out.println("正在执行 ‘"+name+" ’拦截器的动作========"+"执行完登录的时间为:"+new Date());22: //取得执行完Action的时间23: long end = System.currentTimeMillis();24: System.out.println("正在执行 ‘"+name+"’ 拦截器的动作========"+"Action执行了"+(end-start)+"毫秒");25: return result;26: }27: public String getName() {28: return name;29: }30:31: public void setName(String name) {32: this.name = name;33: }34: }35:
2、struts.xml
1: <?xml version="1.0" encoding="UTF-8" ?>2: <!DOCTYPE struts PUBLIC3: "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"4: "http://struts.apache.org/dtds/struts-2.3.dtd">5: <struts>6: <package name="struts2" extends="struts-default">7: <!-- 配置应用所使用的拦截器 -->8: <interceptors>9: <!-- 配置mysimple拦截器 -->10: <interceptor name="mysimple" class="cc.openhome.SimpleInterceptor">11: <!-- 为该拦截器指定参数值 -->12: <param name="name">简单拦截器</param>13: </interceptor>14: <interceptor name="authority" class="cc.openhome.AuthorityInterceptor"/>15: </interceptors>16: <action name="login" class="cc.openhome.LoginAction">17: <result name="success">/success.jsp</result>18: <result name="error">/error.jsp</result>19: <result name="input">/denglu.jsp</result>20: <interceptor-ref name="defaultStack"/>21: <interceptor-ref name="mySimple">22: <param name="name">动态拦截器</param>23: </interceptor-ref>24: </action>25: </package>26: </struts>
3、登录页面login.jsp
1: <%@ page language="java" contentType="text/html; charset=UTF-8"2: pageEncoding="UTF-8"%>3: <%@ taglib prefix="s" uri="/struts-tags"%>4: <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">5: <html>6: <head>7: <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">8: <title>登录界面</title>9: </head>10: <body>11: <s:form action="login">12: <s:textfield name="username" label="用户名"/>13: <s:textfield name="password" label="密 码"/>14: <s:submit value="登录"/>15: </s:form>16: </body>17: </html>
4、LoginAction.java
1: package cc.openhome;2: import com.opensymphony.xwork2.Action;3: import com.opensymphony.xwork2.ModelDriven;4: public class LoginAction implements Action{5: private String username;6: private String password;7: public String getUsername() {8: return username;9: }10: public void setUsername(String username) {11: this.username = username;12: }13: public String getPassword() {14: return password;15: }16: public void setPassword(String password) {17: this.password = password;18: }19: @Override20: public String execute() throws Exception {21: // TODO Auto-generated method stub22: return SUCCESS;23: }24: }25:
5、运行结果
2、拦截器的执行顺序
修改struts.xml如下
1: <?xml version="1.0" encoding="UTF-8" ?>2: <!DOCTYPE struts PUBLIC3: "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"4: "http://struts.apache.org/dtds/struts-2.3.dtd">5: <struts>6: <constant name="struts.custom.i18n.resources" value="messageResource"></constant>7: <package name="openhome" extends="struts-default">8: <!-- 配置应用所使用的拦截器 -->9: <interceptors>10: <!-- 配置mysimple拦截器 -->11: <interceptor name="mysimple" class="cc.openhome.SimpleInterceptor">12: <!-- 为该拦截器指定参数值 -->13: <param name="name">简单拦截器</param>14: </interceptor>15: </interceptors>16: <action name="login" class="cc.openhome.LoginAction" method="execute">17: <result name="input">/input.jsp</result>18: <result name="success">/WEB-INF/page/welcome.jsp</result>19: <result name="error">/WEB-INF/page/error.jsp</result>20: <interceptor-ref name="defaultStack" />21: <interceptor-ref name="mysimple">22: <param name="name">第一个动态拦截器</param>23: </interceptor-ref>24: <interceptor-ref name="mysimple">25: <param name="name">第二个动态拦截器</param>26: </interceptor-ref>27: </action>28: </package>29: <!-- Add packages here -->30: </struts>31:
运行结果如下:可以看到先配置的拦截器,先获得执行机会。
3、方法拦截器
如果不需要拦截特定的方法,假如不拦截execute方法,则在控制台上就不会输出任何信息。
只需要修改提供方法过滤的拦截器
MyFilterInterceptor.java
1: package cc.openhome;2: import java.util.Date;3: import com.opensymphony.xwork2.ActionInvocation;4: import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;5: public class MyFilterInterceptor extends MethodFilterInterceptor {6: private String name;7: public String getName() {8: return name;9: }10: public void setName(String name) {11: this.name = name;12: }13: @Override14: protected String doIntercept(ActionInvocation invocation) throws Exception {15: // TODO Auto-generated method stub16: //取得被拦截的Action实例17: LoginAction action = (LoginAction) invocation.getAction();18:19: //打印20: System.out.println("正在执行 ‘"+name+"’ 拦截器的动作========"+"开始执行登录的时间为:"+new Date());21:22: //取得开始执行Action的时间23: long start = System.currentTimeMillis();24:25: //执行该拦截器的后一个拦截器,或者直接指定Action的execute()方法26: String result = invocation.invoke();27: System.out.println("正在执行 ‘"+name+" ’拦截器的动作========"+"执行完登录的时间为:"+new Date());28:29: //取得执行完Action的时间30: long end = System.currentTimeMillis();31: System.out.println("正在执行 ‘"+name+"’ 拦截器的动作========"+"Action执行了"+(end-start)+"毫秒");32: return result;33: }34: }35:
struts.xml
1: <?xml version="1.0" encoding="UTF-8" ?>2: <!DOCTYPE struts PUBLIC3: "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"4: "http://struts.apache.org/dtds/struts-2.3.dtd">5: <struts>6: <package name="openhome" extends="struts-default">7: <!-- 配置应用所使用的拦截器 -->8: <interceptors>9: <!-- 配置myfilter拦截器 -->10: <interceptor name="myfilter" class="cc.openhome.MyFilterInterceptor">11: <!-- 为该拦截器指定参数值 -->12: <param name="name">方法过滤拦截器</param>13: </interceptor>14: </interceptor>15: <interceptor name="authority" class="cc.openhome.AuthorityInterceptor"/>16: </interceptors>17: <action name="login" class="cc.openhome.LoginAction" method="execute">18: <result name="input">/input.jsp</result>19: <result name="success">/WEB-INF/page/welcome.jsp</result>20: <result name="error">/WEB-INF/page/error.jsp</result>21: <interceptor-ref name="defaultStack" />22: <interceptor-ref name="myfilter">23: <param name="name">改名后的方法拦截器</param>24: <param name="excludeMethods">execute</param>25: </interceptor-ref>26: </action>27: </package>28: <!-- Add packages here -->29: </struts>30:
3、拦截结果的监听器
为了更精确的定义在execute方法执行前后的结构,以及执行后再处理Result执行的动作。这个监听器通过手动注册在拦截内部,必须实现PreReslutListener接口。
MyPreResultListener.java
1: package cc.openhome;2: import com.opensymphony.xwork2.ActionInvocation;3: import com.opensymphony.xwork2.interceptor.PreResultListener;4: public class MyPreResultListener implements PreResultListener {5: @Override6: public void beforeResult(ActionInvocation invocation, String resultCode) {7: // TODO Auto-generated method stub8: System.out.println("返回的逻辑视图是:"+resultCode);9: }10: }11:
BeforeResultInterceptor.java
1: package cc.openhome;2: import com.opensymphony.xwork2.ActionInvocation;3: import com.opensymphony.xwork2.interceptor.AbstractInterceptor;4: public class BeforeResultInterceptor extends AbstractInterceptor {5: @Override6: public String intercept(ActionInvocation invocation) throws Exception {7: // TODO Auto-generated method stub8: //将一个拦截器结果的监听器注册给该拦截器9: invocation.addPreResultListener(new MyPreResultListener());10: System.out.println("execute方法执行之前的拦截。。。");11: //调用下一个拦截器,或者Action的执行方法execute()12: String result = invocation.invoke();13: System.out.println("execute方法执行之后的拦截。。。");14: return result;15: }16: }17:
struts.xml
1: <?xml version="1.0" encoding="UTF-8" ?>2: <!DOCTYPE struts PUBLIC3: "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"4: "http://struts.apache.org/dtds/struts-2.3.dtd">5: <struts>6: <constant name="struts.custom.i18n.resources" value="messageResource"></constant>7: <package name="openhome" extends="struts-default">8: <!-- 配置应用所使用的拦截器 -->9: <interceptors>10: <!-- 配置myresult拦截器 -->11: <interceptor name="myresult" class="cc.openhome.BeforeResultInterceptor">12: <!-- 为该拦截器指定参数值 -->13: <param name="name">结果的拦截器</param>14: </interceptor>15: </interceptors>16: <action name="login" class="cc.openhome.LoginAction" method="execute">17: <result name="input">/input.jsp</result>18: <result name="success">/WEB-INF/page/welcome.jsp</result>19: <result name="error">/WEB-INF/page/error.jsp</result>20: <interceptor-ref name="defaultStack" />21: <interceptor-ref name="myresult">22: <param name="name">结果拦截器</param>23: </interceptor-ref>24: </action>25: <!-- Add packages here -->26: </struts>27:
运行结果