1.不执行Action,直接在拦截器中改变返回的ResultCode
package com.zucc.interceptor;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
public class TestInterceptor extends AbstractInterceptor{
public TestInterceptor(){
System.out.println("TestInterceptor constructor....");
}
@Override
public String intercept(ActionInvocation invocation) throws Exception {
System.out.println("TestInterceptor intercept....");
return "error";//输入你想返回的ResultCode
}
}
2.执行Action,通过实现PreResultListener接口来返回ResultCode
先新建接口实现类去实现PreResultListener接口
package com.zucc.interceptor;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.PreResultListener;
public class MyPreResultListener implements PreResultListener {
/*
* 在Action的ResultCode返回之前执行,在这里可以改变ResultCode影响返回的Result
*/
@Override
public void beforeResult(ActionInvocation invocation, String resultCode) {
// TODO Auto-generated method stub
System.out.println("MyPreResultListener beforeResult....");
invocation.setResultCode("error");//输入你想返回的ResultCode
}
}
package com.zucc.interceptor;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;
public class TestInterceptor extends AbstractInterceptor{
private MyPreResultListener prl;
public TestInterceptor(){
System.out.println("TestInterceptor constructor....");
prl = new MyPreResultListener();
}
@Override
public String intercept(ActionInvocation invocation) throws Exception {
System.out.println("TestInterceptor intercept....");
invocation.addPreResultListener(prl);
String resultName = invocation.invoke();
return resultName;
}
}