Struts2 拦截器 和 Java 中的动态代理
今天把上个星期看的关于 Struts2 拦截器 总结了一下.
?
这里有一些详细信息可以参考一下.
http://blog.csdn.net/lywtc128/archive/2009/01/06/3720892.aspx
?
?
有三种实现方式
1
public class FirstInterceptor extends MethodFilterInterceptor
?它可以设置 类似于 黑名单,白名单
2
public class SecondInterceptor extends AbstractInterceptor
?它是通过继承一个抽象类来实现的 算是个简易版的
3
public class ThirdInterceptor implements Interceptor
通过这种直接的方式实现.
?
在Struts.xml 里配制:
<interceptors><interceptor name="MethodFilterInterceptor"/><interceptor-ref name="AbstractInterceptor" /><interceptor-ref name="Interceptor" /></interceptor-stack><interceptor-stack name="defaultPrj"><interceptor-ref name="stack"><!-- 给拦截器栈加参 --><param name="MethodFilterInterceptor.value">1</param><param name="AbstractInterceptor.value">2</param><param name="Interceptor.value">3</param></interceptor-ref><!-- defaultStack 是默认的,包括了许多(i18N),最好是带上,而且放到最里面 --><interceptor-ref name="defaultStack" /></interceptor-stack></interceptors><!-- 设置默认拦截器 --><default-interceptor-ref name="defaultPrj"></default-interceptor-ref>
?当然,还少不了监听
public class MyListener implements PreResultListener {public void beforeResult(ActionInvocation ai, String result) {System.out.println("MyListener.beforeResult " + result);}}
?在这里设置
public String intercept(ActionInvocation ai) throws Exception {// 注册监听器ai.addPreResultListener(new MyListener());// 获取参数Map<String, Object> map = ai.getInvocationContext().getParameters();String[] userName = (String[]) map.get("userName");if (null != userName && null != userName) {userName[0] = userName[0] + value + "-*-AbstractInterceptor-*-";}// 执行代理System.out.println("SecondInterceptor.intercept begin");String result = ai.invoke();System.out.println("SecondInterceptor.intercept end");return result;}
?
?
?
上传的代码里有更详细的例子.? 这里就不多说了.