简体   繁体   English

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

[英]Get a specific parameter sent from view in interceptor

Currently using this code to fetch value of the parameter "csrfPreventionSalt" of Interceptor in Struts2. 当前使用此代码在Struts2中获取Interceptor的参数"csrfPreventionSalt"的值。

Can anyone please tell a direct way to fetch its value... 任何人都可以告诉直接获取价值的方法...

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

Assume a parameter was sent to the action, not to the interceptor. 假定参数已发送给操作,而不是发送给拦截器。 When an action is invoked the action context is created and parameters from the request are copied to the action context. 调用动作时,将创建动作上下文,并将请求中的参数复制到动作上下文。 You can get parameters via 您可以通过获取参数

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];
    ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM