PreResultListener拦截器是在action方法执行后,返回result之前执行的拦截器。
MyAction.java
package com.struts2.interceptor;
import com.opensymphony.xwork2.ActionSupport;
public class MyAction extends ActionSupport {
/**
*
*/
private static final long serialVersionUID = 1L;
@Override
public String execute() throws Exception {
System.out.println("execute方法");
return SUCCESS ;
}
}
MyAction.java
package com.struts2.interceptor;
import com.opensymphony.xwork2.ActionSupport;
public class MyAction extends ActionSupport {
/**
*
*/
private static final long serialVersionUID = 1L;
@Override
public String execute() throws Exception {
System.out.println("execute方法");
return SUCCESS ;
}
}
一个简单例子,打印简单的一句话,加上一个简单的拦截器
MyInterceptor.java
package com.struts2.interceptor;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
public class MyInterceptor extends AbstractInterceptor {
/**
*
*/
private static final long serialVersionUID = 1L;
@Override
public String intercept(ActionInvocation invo) throws Exception {
invo.addPreResultListener(new PreInter()) ;
System.out.println("拦截器前执行");
String result = invo.invoke() ;
System.out.println("拦截器后执行");
return result ;
}
}
MyInterceptor.java
package com.struts2.interceptor;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
public class MyInterceptor extends AbstractInterceptor {
/**
*
*/
private static final long serialVersionUID = 1L;
@Override
public String intercept(ActionInvocation invo) throws Exception {
invo.addPreResultListener(new PreInter()) ;
System.out.println("拦截器前执行");
String result = invo.invoke() ;
System.out.println("拦截器后执行");
return result ;
}
}
加上preResult拦截器:
PreInter.java
package com.struts2.interceptor;
import com.opensymphony.xwork2.ActionInvocation;
public class PreInter implements PreResultListener {
public void beforeResult(ActionInvocation arg0, String arg1) {
System.out.println("结果集返回前执行");
}
}
PreInter.java
package com.struts2.interceptor;
import com.opensymphony.xwork2.ActionInvocation;
public class PreInter implements PreResultListener {
public void beforeResult(ActionInvocation arg0, String arg1) {
System.out.println("结果集返回前执行");
}
}
运行结果:
拦截器前执行
execute方法
结果集返回前执行
拦截器后执行