Changing request parameter value in Struts2 interceptor

℡╲_俬逩灬. 提交于 2019-12-31 04:39:09

问题


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();
}

回答1:


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();
}



回答2:


I had a similar problem in my code, but the solution above didn't work for me.

If you want to make changes to any parameters in the Interceptor before they get to the action class use this code:

@Override
public String intercept(ActionInvocation ai) throws Exception {

    ValueStack stack=ai.getStack(); 
    Iterator it =  stack.getRoot().iterator();
    while( it.hasNext() )
    {
        Object objecto = it.next();
        //LoginUsuario is my action class
        if( objecto instanceof LoginUsuario )
        {
            LoginUsuario usuario = (LoginUsuario)objecto;
            usuario.setUsername( usuario.getUsername().toUpperCase() );
            usuario.setPassword( usuario.getPassword().toUpperCase() );
        }
    }
    return ai.invoke();
}


来源:https://stackoverflow.com/questions/30076242/changing-request-parameter-value-in-struts2-interceptor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!