Struts拦截器
2017-03-01 00:00
369 查看
Struts拦截器
struts.xml配置文件中注册:<interceptor name="interceptorName" class="interceptorClass" /> |
<interceptor name="interceptorName" class="interceprotClass"> <param name="paramName">paramValue</param> </interceptor> |
<interceptors> <interceptor name="interceptor1" class="interceptorClass" /> <interceptor name="interceptor2" class="interceprotClass" /> <interceptor-stack name="myStack"> <interceptor-ref name="interceptor1" /> <interceptor-ref name="interceptor2" /> </interceptor-stack> </interceptors> |
<default-interceptor-ref name="myStack" /> |
只要自定义包继承了struts-default默认包,就会继承其内置的各个拦截器和其默认的拦截器栈。struts-dafault包的默认拦截器栈会作用于所有的这些Action。
使用拦截器时,需要在Action中进行配置,通过<interceptor-ref>标签类指定在Action中使用的拦截器。
<action name="login" class="com.chen.action.LoginAction"> <result name="input">/login.jsp</result> <interceptor-ref name="interceptor1" /> <interceptor-ref name="interceptor2"> <param name="paramName">paramValue</param> </interceptor-ref> </action> |
Struts2的默认拦截器
alias拦截器
该拦截器的作用是给参数起一个别名,实现Action之间的传值。在struts.xml文件中的<action>标签下,添加以下标签:
<param name="aliases">#{'name1':'value1','name2':'value2'}</param> |
注释:从一个Action调到另一个Action,可以通过设置result元素的type属性来实现。有两种方法:第一种是将type设置为chain结果类型;第二种是将types属性设置为redirectAction类型。
prepare拦截器
该拦截器用来调用实现了Preparable接口的Action的prepare()方法。如果一个Action实现了Preparable接口,则prepare拦截器会在执行execute()方法之前先执行prepare()方法。
由于Action所在的包基本上都继承了struts-default包,因此也会继承其默认拦截器栈defaultStack,而在defaultStack中配置了prepare拦截器,所以prepare拦截器不需要在struts.xml文件中配置,就会被正确的调用。
timer拦截器
timer拦截器的功能是输出调用Action所需要的时间,它记录了execute方法和在timer后面定义的其他拦截器的intercept方法的执行时间之和,其时间单位为毫秒。<action name="login" class="com.chen.action.LoginAction"> <interceptor-ref name="timer" /> <result name="input">/login.jsp</result> </action> |
自定义拦截器
Struts2拦截器在访问某个Action 方法之前或之后实施拦截。Struts2 拦截器是可插拔的, 拦截器是 AOP 的一种实现。
编写自定义拦截器
自定义拦截器有两种定义方法,一种是要实现com.opensymphony.xwork2.interceptor.Interceptor接口;另一种是继承AbstractInterceptor类来定义拦截器。(1)实现Interceptor接口定义拦截器:
publicclass PermissionInterceptor implements Interceptor { privatefinalstaticlongserialVersionUID=1L; @Override publicvoid destroy() { } @Override publicvoid init() { } @Override public String intercept(ActionInvocation invocation) throws Exception { String result=null; //提取session[username] Object username=ActionContext.getContext().getSession().get("username"); //验证是否登录(若登录会存在session) if(username!=null&&!username.equals("")){ result=invocation.invoke(); }else{ result="login"; } System.out.println(result); returnresult; } } |
publicclass MyInterceptor extends AbstractInterceptor { @Override public String intercept(ActionInvocation invocation) throws Exception { //调用下一个拦截器 String result=invocation.invoke(); returnresult; } } |
配置自定义拦截器
在struts.xml文件中的<package>标签下:<interceptors> <interceptor name="mylogin" class="com.chen.interceptor.PermissionInterceptor"></interceptor> </interceptors> |
方法过滤拦截器
继承MethodFilterInterceptor类的子类能够有选择的拦截Action中的某个指定方法。继承MethodFilterInterceptor类的子类必须实现doIntercept()方法,用来实现真正的拦截逻辑。
编写方法过滤拦截器:
publicclass MyMethodFilterInterceptor extends MethodFilterInterceptor { @Override protected String doIntercept(ActionInvocation invocation) throws Exception { String result=invocation.invoke(); returnresult; } } |
<interceptors> <interceptor name="FilterInterceptor" class="com.chen.interceptor.MyMethodFilterInterceptor" /> </interceptors> <action name="filterAction" class="com.chen.action.FilterAction"> <result name="success">/success.jsp</result> <interceptor-ref name="FilterInterceptor"> <!-- 指定拦截方法 --> <param name="includeMethods">execute,method1</param> <!-- 指定不拦截方法 --> <param name="excludeMethods">method2</param> </interceptor-ref> </action> |
相关文章推荐
- Struts 2的基石——拦截器(Interceptor)
- struts-3-拦截器
- Java Web整合开发(19) -- Struts 2.x 拦截器与文件上传
- Struts 2 拦截器
- struts文件上传拦截器maximumSize设置文件大小不起作用
- 使用Spring内置拦截器为Struts的Action在每个方法调用之前打印一句话
- Struts 2的基石——拦截器(Interceptor)
- Struts拦截器拦截未登录的操作
- javaweb国际化,用struts标签解决,注意每个页面都是action跳jsp,并且拦截器的配置使得每次将国际化配置保存在session
- struts 拦截器和过滤器的区别和原理
- struts-拦截器
- 关于Struts 2 拦截器参数丢失问题
- struts 拦截器 interceptor
- Struts 2的基石——拦截器(Interceptor)
- struts拦截器详解
- struts action和拦截器
- Struts---拦截器
- 用struts的拦截器实现用户权限控制