[英]Struts2 change form field using interceptor
I am newbie in struts2.我是struts2的新手。 In interceptor how can i change value of form field and then submitting it to database?
在拦截器中,如何更改表单字段的值然后将其提交到数据库? For example when user enters
firstName
value in form then when it submits i want to change firstName
and then submit it to database.例如,当用户在表单中输入
firstName
值时,当它提交时,我想更改firstName
,然后将其提交到数据库。
Here is my interceptor's code这是我的拦截器的代码
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;
}
}
In your code simply you need to change value in the map.在您的代码中,您只需更改地图中的值。 no need to put any other context.
无需放置任何其他上下文。
Map<String, Object> params = actionInvocation.getInvocationContext().getParameters();
params.put("firstName", "Changed");
Try this:尝试这个:
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();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.