struts2在拦截器中改变返回的ResultCode

来源:互联网 发布:卫星电视直播软件 编辑:程序博客网 时间:2024/06/10 18:46

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....");}@Overridepublic 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 */@Overridepublic void beforeResult(ActionInvocation invocation, String resultCode) {// TODO Auto-generated method stubSystem.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();}@Overridepublic String intercept(ActionInvocation invocation) throws Exception {System.out.println("TestInterceptor intercept....");invocation.addPreResultListener(prl);String resultName = invocation.invoke();return resultName;}}



0 0