拦截器的三种实现方式!

拦截器的第一种实现方式:直接实现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
init value ~! /usernameException.jsp /passwordException.jsp /result.jsp /login.jsp /result.jsp /login.jsp /test2.jsp /test2.jsp action2 ${username} ${password} ${usernameAndPassword} hello world !!! /action2.jsp /action3.jsp execute,myExecute myExecute /ModenAccept.jsp /action3.jsp /ModenAccept.jsp getPassword,execute


你可能感兴趣的:(拦截器的三种实现方式!)