1

目前使用此代码获取"csrfPreventionSalt"Struts2中Interceptor的参数值。

谁能告诉一个直接的方法来获取它的价值......

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];
            }
        }
    }
}
4

1 回答 1

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];
    ...
于 2016-02-15T19:27:48.323 回答