Chinaunix首页 | 论坛 | 博客
  • 博客访问: 335248
  • 博文数量: 96
  • 博客积分: 2041
  • 博客等级: 大尉
  • 技术积分: 1080
  • 用 户 组: 普通用户
  • 注册时间: 2012-01-20 14:08
文章分类

全部博文(96)

文章存档

2015年(2)

2013年(1)

2012年(93)

分类: 系统运维

2012-01-24 12:57:39

invocation.invoke();//若后面还有拦截器,则急需调用后面的拦截器,若没有则调用action

 

TheInterceptor2.java

package com.shengsiyuan.interceptor;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

public class TheInterceptor2 extends AbstractInterceptor
{

    @Override
    public String intercept(ActionInvocation invocation) throws Exception
    {
        System.out.println("interceptor before...");
        String result = invocation.invoke();
        System.out.println("interceptor after...");
        return result ;
    }

}

 

struts.xml


    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    ">

   
       
           
                     shengsiyuan
                    

           
       

       
            /usernameInvalid.jsp
            /passwordInvalid.jsp
       

       
           
           
       

       
            /result.jsp
            /login.jsp
       

       
           
            action2
            ${username}
            ${password}
            ${usernameAndPassword}
           
           
       
           
           
            
       

      
           /tokenSuccess.jsp
           tokenFail.jsp
    
    
      

      
       /action2.jsp
      

       
            /result.jsp
       

       
            /output.jsp
       

       
            /output.jsp
       

       
            /registerResult.jsp
            /register.jsp
       

   

显示结果

before…

interceptor before…

interceptor after…

after…

 

若在TheInterceptor1和TheInterceptor2中的interceptor方法加上

System.out.println("interceptro1:"+invocation.getAction()+getClass());

System.out.println("interceptro2:"+invocation.getAction()+getClass());

则显示结果都是:class.shengsiyuan.struts2.Action1

过滤器能过滤所有东西,拦截器只能拦截action(中的execute方法)

定义拦截器时可以直接继承AbstractInterceptor抽象类(该类实现了interceptro接口,并且对init和destory方法进行了空实现),然后实现其抽象方法interceptor即可。

方法过滤拦截器(可以对制定方法进行拦截的拦截器):

如果既没有includeMethods参数,也没有指定execludeMethods参数,那么所有的方法都会被拦截,也就是说所有的方法都被默认是includeMedthods;如果仅仅指定了(mehod = “…”)includMethods,那么只会拦截includeMethods中的方法,没有包含在includeMethods中的方法就不会被拦截。

 

Action1.jsp

package com.shengsiyuan.struts2;

import com.opensymphony.xwork2.ActionSupport;

public class Action1 extends ActionSupport
{
    private String    username;
    private String    password;
    private String    usernameAndPassword;

    public String getUsernameAndPassword()
    {
        return usernameAndPassword;
    }

    public void setUsernameAndPassword(String usernameAndPassword)
    {
        this.usernameAndPassword = usernameAndPassword;
    }

    public String getUsername()
    {
        return username;
    }

    public void setUsername(String username)
    {
        this.username = username;
    }

    public String getPassword()
    {
        return password;
    }

    public void setPassword(String password)
    {
        this.password = password;
    }

    @Override
    public String execute() throws Exception
    {
        this.usernameAndPassword = this.username + this.password;
        return SUCCESS;
    }

    public String myExecute() throws Exception
    {
        System.out.println("myExecute invoke");
        return SUCCESS ;
    }
}

struts.xml


    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    ">

   
       
           
                     shengsiyuan
                    

           
       
       

       
            /usernameInvalid.jsp
            /passwordInvalid.jsp
       

       
           
           
       

       
            /result.jsp
            /login.jsp
       

       
           
            action2
            ${username}
            ${password}
            ${usernameAndPassword}
           
           
       
           
           
            
             execute
        
    

            
       

      
           /tokenSuccess.jsp
           tokenFail.jsp
    
    
      

      
       /action2.jsp
      

       
            /result.jsp
       

       
            /output.jsp
       

       
            /output.jsp
       

       
            /registerResult.jsp
            /register.jsp
       

   

TheInterceptor3.java

package com.shengsiyuan.interceptor;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;

public class TheInterceptor3 extends MethodFilterInterceptor
{

    @Override
    protected String doIntercept(ActionInvocation invocation) throws Exception
    {
        System.out.println("before interceptor3");
        String result = invocation.invoke() ;
        System.out.println("after interceptor3");
        return result ;
    }    

}

action1.jsp

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
 
 

   


    username:

    password:

   
   
>
 

阅读(1496) | 评论(0) | 转发(0) |
0

上一篇:拦截器Interceptor

下一篇:struts2监听器

给主人留下些什么吧!~~