steven35 steven35 - 1 month ago 16
Java Question

Changing request parameter value in Struts2 interceptor

Does anybody know if it is possible to change/remove request parameter values in a Struts2 interceptor?

The request parameter

Map
is an instance of
UnmodifiableMap
so it doesn't look like it can be manipulated with in the interceptor.

UPDATE:

I'm using Liferay so
uParamsMap
will be an
UnmodifiableMap


public String intercept(ActionInvocation invocation) throws Exception {
final ActionContext context = invocation.getInvocationContext();
PortletRequest request = (PortletRequest) context.get(REQUEST);
Map<String, String[]> uParamsMap = request.getParameterMap();
return invocation.invoke();
}

Answer

May be you can try as 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();
}
Comments