Struts2的拦截器

1.拦截器的重要性

Struts2中的很多功能都是由拦截器完成的。比如:servletConfig,staticParam,params,modelDriven等等。
是AOP编程思想的一种应用形式。

2.拦截器的执行时机

Struts2的拦截器_第1张图片
时机.png

3.自定义拦截器

3.1、拦截器的类试图
Struts2的拦截器_第2张图片
自定义拦截器.png
3.2编写步骤
  • a.编写一个类,继承AbstractInterceptor类或者实现Interceptor接口。重写intercept方法
public class Demo1Interceptor extends AbstractInterceptor {
    @Override
    public String intercept(ActionInvocation actionInvocation) throws Exception {
        System.out.println("拦截前");
        //放行
        String invoke = actionInvocation.invoke();
        System.out.println("拦截后");
        return invoke;
    }
}
  • b.配置拦截器:注意拦截器必须先声明再使用

        
        
            
        
        
            
            /success.jsp
        
    
3.3.执行顺序
public class Demo1Action extends ActionSupport {

    public String save() throws Exception {
        System.out.println("doAction");
        return "success";
    }
}
拦截前
doAction
拦截后
3.4.多个拦截器

        
        
            
            
        
        
            
            
            /success.jsp
        
    

4.拦截器的应用

public class Demo1Interceptor extends MethodFilterInterceptor {

    @Override
    protected String doIntercept(ActionInvocation actionInvocation) throws Exception {
        System.out.println("拦截前");
        //放行
        String invoke = actionInvocation.invoke();
        System.out.println("拦截后");
        return invoke;
    }
}
 
    
        
        
            
            
                
                
            
        
        

        
            /success.jsp
        

        
            
                save
            
            /success.jsp
        
    
拦截器类视图(全)
Struts2的拦截器_第3张图片
自定义拦截器.png

你可能感兴趣的:(Struts2的拦截器)