分类: 系统运维
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
action2
${username}
${password}
${usernameAndPassword}
显示结果
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
action2
${username}
${password}
${usernameAndPassword}
execute
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"%>
>