对于需要登陆验证、权限验证等功能的网站,每一次请求,每一个action都写一段验证的代码,未免显得冗余且不易维护。struts2提供了拦截器interceptor,为这些页面提供一个切面,或者说公共组件,以达到易维护、提高代码重用率的目的。struts默认的interceptor有很多,就不去一一列举,这里从自定义拦截器开始学习。
1、定义一个自定义拦截器:
package com.owlforest.home.interceptor; import com.opensymphony.xwork2.ActionInvocation; import com.opensymphony.xwork2.interceptor.AbstractInterceptor; public class MyInterceptor extends AbstractInterceptor { public String intercept(ActionInvocation invocation) throws Exception { String output = "Pre-Processing"; System.out.println(output); String result = invocation.invoke(); output = "Post-Processing"; System.out.println(output); return result; } }
2、修改struts.xml,在package中声明和使用拦截器MyInterceptor
<package name="suibian" extends="struts-default"> <!--声明自定义的拦截器--> <interceptors> <interceptor name="myinterceptor" class="com.owlforest.home.interceptor.MyInterceptor" /> </interceptors> <action name="hello" class="com.owlforest.home.action.HelloWorldAction" method="excute"> <!--使用自定义拦截器--> <interceptor-ref name="myinterceptor" /> <interceptor-ref name="params"/> <result name="success">/HelloWorld.jsp</result> </action> </package>
该拦截器在com.owlforest.home.action.HelloWorldAction执行前后会生效,为了便于理解,附上该Action的代码:
package com.owlforest.home.action; import com.opensymphony.xwork2.ActionSupport; public class HelloWorldAction extends ActionSupport { private String name; public String excute(){ System.out.println("excute"); return "success"; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
3、执行程序
程序实现参照Struts2学习:HelloWorld
执行action过后可查看控制台输出,看到下面的信息后及说明生效。
4、对于一个struts.xml里面的package,可能有多个action,多个action可能会用到一系列的相同的interceptor,如果每个action都要写一系列相同的拦截器,则很难维护。struts2提供了拦截器栈,来帮助整合这些拦截器,达到复用的目的。
为了方便理解,我写了两个自定义的拦截器MyInterceptor和SecInterceptor
package com.owlforest.home.interceptor; import com.opensymphony.xwork2.ActionInvocation; import com.opensymphony.xwork2.interceptor.AbstractInterceptor; public class MyInterceptor extends AbstractInterceptor { public String intercept(ActionInvocation invocation) throws Exception { String output = "Pre-Processing 拦截器MyInterceptor"; System.out.println(output); String result = invocation.invoke(); output = "Post-Processing 拦截器MyInterceptor"; System.out.println(output); return result; } }
package com.owlforest.home.interceptor; import com.opensymphony.xwork2.ActionInvocation; import com.opensymphony.xwork2.interceptor.AbstractInterceptor; public class SecInterceptor extends AbstractInterceptor { public String intercept(ActionInvocation invocation) throws Exception { String output = "Pre-Processing SecInterceptor"; System.out.println(output); String result = invocation.invoke(); output = "Post-Processing SecInterceptor"; System.out.println(output); return result; } }
配置struts.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <!-- 设置struts是否为开发模式,默认为false,测试阶段一般设为true. --> <constant name="struts.devMode" value="true"/> <package name="suibian" extends="struts-default"> <interceptors> <interceptor name="myinterceptor" class="com.owlforest.home.interceptor.MyInterceptor" /> <interceptor name="secinterceptor" class="com.owlforest.home.interceptor.SecInterceptor" /> <interceptor-stack name="myinterceptorstack"> <interceptor-ref name="myinterceptor" /> <interceptor-ref name="secinterceptor" /> </interceptor-stack> </interceptors> <action name="hello" class="com.owlforest.home.action.HelloWorldAction" method="excute"> <interceptor-ref name="myinterceptorstack"/> <interceptor-ref name="params"/> <result name="success">/HelloWorld.jsp</result> </action> </package> </struts>
5、因为拦截器和拦截器栈是在package中声明使用的,作用域仅在当前声明的package中,对于一些验证、异常相关的拦截器,需要在全局、也就是所有的package中都能使用,这种需要定义一个全局的package,使相应的action对应的package继承自该全局package即可。如下:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <!-- 设置struts是否为开发模式,默认为false,测试阶段一般设为true. --> <constant name="struts.devMode" value="true"/> <!--全局的package--> <package name="commominterceptor" extends="struts-default"> <interceptors> <interceptor name="myinterceptor" class="com.owlforest.home.interceptor.MyInterceptor" /> <interceptor name="secinterceptor" class="com.owlforest.home.interceptor.SecInterceptor" /> <interceptor-stack name="myinterceptorstack"> <interceptor-ref name="myinterceptor" /> <interceptor-ref name="secinterceptor" /> </interceptor-stack> </interceptors> </package> <!--继承自commominterceptor的package--> <package name="suibian" extends="commominterceptor"> <action name="hello" class="com.owlforest.home.action.HelloWorldAction" method="excute"> <interceptor-ref name="myinterceptorstack"/> <interceptor-ref name="params"/> <result name="success">/HelloWorld.jsp</result> </action> </package> </struts>
拦截器栈与全局拦截器的运行效果如下:
原文地址:https://www.cnblogs.com/wsfu/p/10356541.html