拦截器的三种实现方式!
来源:互联网 发布:好的交友软件 编辑:程序博客网 时间:2024/05/21 04:01
拦截器的第一种实现方式:直接实现interceptor
拦截器的第二种实现方式:继承AbstractInterceptor (AbstractInterceptor 实现了interceptor接口,并对init,destory进行了空实现)
public class TheInterceptor1 implements Interceptor {public void destroy() {System.out.println("destroy invoked !!");}public void init() {System.out.println("init invoked !!!");}public String intercept(ActionInvocation invocation) throws Exception {System.out.println("intercept1 invoked before !!!");String value = invocation.invoke();System.out.println("intercept1 invoked after !!!");return value;}}
拦截器的第三种实现方式:继承MethodsFilterInterceptor (对指定方法进行拦截)
public class TheInterceptor3 extends MethodFilterInterceptor {protected String doIntercept(ActionInvocation invocation) throws Exception {invocation.addPreResultListener(new TheListener());System.out.println("doIntercept3 invoked before !!!");invocation.invoke();System.out.println("doIntercept3 invoked after !!!");return null;}}
配置文件: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><package name="struts2" extends="struts-default"><interceptors><interceptor name="theintercept1" class="db.struts.interceptor.TheInterceptor1"><param name="test">init value ~!</param></interceptor><interceptor name="theintercept2" class="db.struts.interceptor.TheInterceptor2"></interceptor><interceptor name="theintercept3" class="db.struts.interceptor.TheInterceptor3"></interceptor><interceptor name="theintercept4" class="db.struts.interceptor.TheInterceptor4"></interceptor></interceptors><global-results><result name="usernameExceptione">/usernameException.jsp</result><result name="passwordException">/passwordException.jsp</result></global-results><global-exception-mappings><exception-mapping result="usernameExceptione"exception="UsernameException"></exception-mapping><exception-mapping result="passwordException"exception="PasswordException"></exception-mapping></global-exception-mappings><action name="login" class="db.my.struts.loginAction"><result name="success">/result.jsp</result><result name="input">/login.jsp</result></action><action name="login2" class="db.my.struts.loginAction2"><result name="success">/result.jsp</result><result name="input">/login.jsp</result></action><action name="userAction" class="db.my.struts.UserAction"><result name="success">/test2.jsp</result></action><action name="userAction2" class="db.my.struts.UserAction2"><result name="success">/test2.jsp</result></action><action name="action1" class="db.my.struts.Action1"><result name="success" type="redirectAction"><param name="actionName">action2</param><param name="username">${username}</param><param name="password">${password}</param><param name="usernameAndPassword">${usernameAndPassword}</param><param name="kaku">hello world !!!</param></result></action><action name="action2" class="db.my.struts.Action2" method="myExecute"><!--action 指定了method方法,拦截器就会只拦截指定的这个method方法!--><result name="success">/action2.jsp</result><result name="invalid.token">/action3.jsp</result><interceptor-ref name="token"></interceptor-ref><interceptor-ref name="theintercept1"></interceptor-ref><interceptor-ref name="theintercept2"></interceptor-ref><interceptor-ref name="theintercept3"><param name="includeMethods">execute,myExecute</param><param name="excludeMethods">myExecute</param></interceptor-ref><interceptor-ref name="defaultStack"></interceptor-ref></action><action name="moden" class="db.my.struts.ModenAction"><result name="success">/ModenAccept.jsp</result><result name="input">/action3.jsp</result></action><action name="action4" class="db.my.struts.action4"><result name="success">/ModenAccept.jsp</result><interceptor-ref name="theintercept4"><param name="includeMethods">getPassword,execute</param></interceptor-ref><interceptor-ref name="defaultStack"></interceptor-ref></action></package></struts>
- 拦截器的三种实现方式!
- struts2拦截器的三种实现方式
- struts2中拦截器实现的三种方式
- 实现拦截器的三种方法
- 实现拦截器的三种方法
- struts自定义拦截器的三种方式
- SpringMVC 拦截器不拦截静态资源的三种处理方式方法
- SpringMVC 拦截器不拦截静态资源的三种处理方式
- SpringMVC 拦截器不拦截静态资源的三种处理方式
- SpringMVC 拦截器不拦截静态资源的三种处理方式
- SpringMVC 拦截器不拦截静态资源的三种处理方式
- SpringMVC 拦截器不拦截静态资源的三种处理方式
- SpringMVC 拦截器不拦截静态资源的三种处理方式
- SpringMVC 拦截器不拦截静态资源的三种处理方式
- 【框架】[Spring]AOP拦截-三种方式实现自动代理
- springMVC 拦截器的两种实现方式
- 拦截请求的三种方式 Filter、Interceptor、Aspect
- 浅谈Android平台短信拦截的几种实现方式
- 给程序员小弟弟小妹妹们的一些总结性忠告
- ARM指令后缀与常用读取指令
- android shape详解
- 简单易懂的hibernate例子
- Alchemy解决方案使得大型主机应用程序能在Windows Azure上运行
- 拦截器的三种实现方式!
- 英文版WordPress如何简单变中文
- 一个struts的action跳转大全!
- 使用java读取配置文件(ini、properties)
- java 的继承原理
- .c文件和.cpp文件导致的编译问题
- struts文件上传,各种类型对应的配置类型大全
- 命名空间重名解决方法
- Linux redhat 9.0 中挂载U盘的方法!