在拦截器中获取从视图发送的特定参数

Get a specific parameter sent from view in interceptor

目前正在使用此代码获取 Struts2 中拦截器的参数 "csrfPreventionSalt" 的值。

任何人都可以告诉直接获取其值的方法...

public String intercept(ActionInvocation invocation) throws Exception {
    final ActionContext context=invocation.getInvocationContext();
    HttpServletRequest httpReq = ServletActionContext.getRequest();
    String salt ="";

    Map<String, Object> params = (Map<String, Object>)ActionContext.getContext().getParameters();
    Iterator<Entry<String, Object>> it = (Iterator<Entry<String, Object>>)params.entrySet().iterator();
    while(it.hasNext()) {
        Entry<String, Object> entry = it.next();
        if(entry.getKey().equals("csrfPreventionSalt"))
        {
        Object obj = entry.getValue();
        if (obj instanceof String[]){
            String[] strArray = (String[]) obj;
            if (strArray!=null) {
                 salt = strArray[0];
            }
        }
    }
}

假设参数被发送到动作,而不是拦截器。调用动作时,将创建动作上下文,并将请求中的参数复制到动作上下文。您可以通过

获取参数
public String intercept(ActionInvocation invocation) throws Exception {
    final ActionContext context = invocation.getInvocationContext(); 
    Map<String, Object> parameters = context.getParameters();
    String[] values = (String[]) parameters.get("csrfPreventionSalt");
    String salt = values[0];
    ...