问题描述
我要写个Interceptor,目的是让输入的值改变,比如:在jsp中,<form action="***!**.action"><input type="text" name ="biaoti" value="biaoti1111"><input type="submit"></form>在传到action之前,我要把“biaoti”到值“biaoti1111” 改成 “biaoti2222”在action直接取biaoti值就得到“biaoti2222” 请问。我在拦截器中该咋写啊???我写的是String ss=request.getParameter("biaoti");但request没有setParameter()方法啊?我还有别的办法吗,难道我只有去action中把得到的值改一般?那样工作量太大了,我写了N个action,都需要此操作!!天啊!!
解决方案
package interceptor;import action.EAction;import com.opensymphony.xwork.Action;import com.opensymphony.xwork.ActionInvocation;import com.opensymphony.xwork.interceptor.AroundInterceptor;public class ConvertInterceptor extends AroundInterceptor { public String intercept(ActionInvocation invocation) throws Exception { before(invocation); String result = invocation.invoke(); after(invocation, result); return result; } protected void after(ActionInvocation actionInvocation, String s) throws Exception { } protected void before(ActionInvocation actionInvocation) throws Exception { Action action = actionInvocation.getAction(); if(action instanceof EAction) { EAction eAction = (EAction) action; eAction.setParam("hello, world");//你要修改的参数名称 } }}假如说下面就是你要修改的Actionpackage action;import com.opensymphony.xwork.ActionSupport;public class EAction extends ActionSupport { public String execute() throws Exception { System.out.println("param = " + param); return SUCCESS; } public String getParam() { return param; } public void setParam(String param) { this.param = param; } private String param;}另外,把这个interceptor放到你的所有的interceptor之后就可以了,例如<interceptors> <interceptor name="convertInterceptor" class="interceptor.ConvertInterceptor"/> <interceptor-stack name="myStack"> <interceptor-ref name="completeStack"/> <interceptor-ref name="convertInterceptor"/> </interceptor-stack></interceptors><default-interceptor-ref name="myStack"/>