javaWeb框架struts2中的方法拦截器(5)

1、 Struts2的方法拦截器概述

Struts2拦截器也可以通过MethodFilterInterceptor类实现,MethodFilterInterceptor重写了AbstractInterceptor类的intercept(ActionInvocationinvocation)方法,但提供了一个doIntercept(ActionInvocation invocation)抽象方法。从这种设计方式可以看出,MethodFilterInterceptor类的intercept已经实现了对Action的拦截行为(只是实现了方法过滤的逻辑),但真正的拦截逻辑还需要开发者提供,也就是通过回调doIntercept方法实现。可见,如果用户需要实现自己的拦截逻辑,则应该重写doIntercept(ActionInvocation invocation)方法。 实现方法过滤的拦截器与实现普通拦截器并没有太大的区别,只需要注意两个地方:实现方法过滤的拦截器需要继承MethodFilterInterceptor抽象类,并且重写doIntercept方法定义对Action的拦截逻辑。

在MethodFilterInterceptor方法中,额外增加了如下两个方法: public void setExcludeMethods(String excludeMethods):排除需要过滤的方法——设置方法“黑名单”,所有在excludeMethods字符串中列出的方法都不会被拦截。 public void setIncludeMethods(String includeMethods):设置需要过滤的方法——设置方法“白名单”,所有在includeMethods字符串中列出的方法都会被拦截。 注意:如果一个方法同时在excludeMethods和includeMethods中列出,则该方法会被拦截。 因为MethodFilterInterceptor类包含了如上的两个方法,则该拦截器的子类也会获得这两个方法。可以在配置文件中指定需要被拦截,或者不需要被拦截的方法。

2、 开发前的准备工作

要有一个能运行的struts2环境

3、 实现继承MethodFilterInterceptor的拦截器的源代码package com.struts2;import com.opensymphony.xwork2.ActionInvocation;import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;/** * 简单实现struts2的拦截方法的拦截器,继承MethodFilterInterceptor抽象类 * * @author 范芳铭 */{// 简单拦截器的名字private String name;// 重写doIntercept方法,实现对Action的拦截逻辑public String doIntercept(ActionInvocation invocation) throws Exception {long start = System.currentTimeMillis();// 执行该拦截器的后一个拦截器,或者直接指定Action的execute方法//LoginAction action = (LoginAction) invocation.getAction();String result = invocation.invoke();long end = System.currentTimeMillis();System.out.println(name + ” ,检测出本Action执行的时间为:” + (end – start) + “毫秒”);return result;}public String getName() {return name;}(String name) {this.name = name;}}4、 配合测试用的Actionpackage com.struts2;import com.opensymphony.xwork2.ActionSupport;/** * 简单实现的action * @author 范芳铭 */{serialVersionUID = 7854497526623985504L;// 主执行方法public String execute() throws Exception {System.out.println(“—LoginAction 被执行。”);return “success”;}//另外一个方法,本方法不要被拦截public String getExcludeMethod() throws Exception {System.out.println(“—getExcludeMethod,我不要被拦截。”);return “success”;}}5、 配合测试的login.jsp>> ======>> =====>> </body></html>6、 Struts.xm的修改<package extends=”struts-default”><!– 定义一个package –><interceptors><interceptor><param>我是struts2的方法拦截器</param><param>getExcludeMethod</param></interceptor><interceptor-stack><interceptor-ref /><interceptor-ref /></interceptor-stack></interceptors><!– 对action返回结果的配置 –><action><result>/index.jsp</result><interceptor-ref></interceptor-ref></action><action method=”getExcludeMethod”><result>/index.jsp</result><interceptor-ref></interceptor-ref></action></package>上面配置文件的代码通过excludeMethods属性指定了execute方法无须被拦截,如果浏览者在浏览器中再次向login的Action发送请求,在Tomcat控制台将看不到任何输出,表明该拦截器没有拦截Action的execute方法。如果需要同时指定多个方法不被该拦截器拦截,则多个方法之间以英文逗号(,)隔开。7、 运行结果

启动中间件,输入 :8080/webStudy/login.jsp 看到两个提交按钮,,从上往下点击,后台输出:

—LoginAction 被执行。 我是struts2的方法拦截器 ,检测出本Action执行的时间为:134毫秒 —getExcludeMethod,我不要被拦截。

还要高声歌唱。那歌声,一定是响遏流云的,

javaWeb框架struts2中的方法拦截器(5)

相关文章:

你感兴趣的文章:

标签云: