我是struts2的新手。
在拦截器中,如何更改表单字段的值,然后将其提交到数据库?
例如,当用户在表单中输入firstName
值时,则在提交时我想要更改firstName
然后将其提交到数据库。
这是我的拦截器代码
public class TestInterceptor extends AbstractInterceptor implements Interceptor
{
@Override
public String intercept(ActionInvocation actionInvocation) throws Exception
{
ValueStack stack = actionInvocation.getStack();
Map<String, Object> params = ActionContext.getContext().getParameters();
Set<String> keys = params.keySet();
System.out.println(keys + " " + stack.size());
/*
* for (String key : keys)
* {
* String[] value = (String[]) params.get(key);
* System.out.println(value.length + " , " + value[0]);
* }
*/
Map<String, Object> context = new HashMap<String, Object>();
context.put("firstNames", "Changed");
context.put("firstName", "Changed");
stack.setParameter("firstName", "Changeds");
stack.push(context);
String result = actionInvocation.invoke();
return result;
}
}
答案 0 :(得分:0)
在您的代码中,您只需更改地图中的值即可。无需提出任何其他背景。
Map<String, Object> params = actionInvocation.getInvocationContext().getParameters();
params.put("firstName", "Changed");
答案 1 :(得分:0)
尝试一下:
public String intercept(ActionInvocation invocation) throws Exception {
final ActionContext context = invocation.getInvocationContext();
Map<String,Object> parameters = (Map<String,Object>)context.get(ActionContext.PARAMETERS);
Map<String, Object> parametersCopy = new HashMap<String, Object>();
parametersCopy.putAll(parameters);
parametersCopy.put("myParam", "changedValue");
context.put(ActionContext.PARAMETERS, parametersCopy);
return invocation.invoke();
}