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

时间:2016-02-15 18:09:32

标签: java parameters struts2 interceptor actioncontext

目前使用此代码获取Struts2中Interceptor的参数"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];
            }
        }
    }
}

1 个答案:

答案 0 :(得分: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];
    ...