在Struts 2中获取拦截器参数

时间:2013-05-08 12:43:07

标签: java parameters struts2 interceptor interceptorstack

我有以下动作映射

<action name="theAction" ...>
...
    <param name="param1">one</param>
    <param name="param2">two</param>
    ...
    <param name="paramN">nth-number</param>
...
</action>

我可以使用拦截器

中的以下行获取参数图
Map<String, Object> params = ActionContext.getContext().getParameters();

如上所述,是否可以获得拦截器参数,如下图所示。

<action name="theAction" ...>
...
    <interceptor-ref name="theInterceptor">
        <param name="param1">one</param>
        <param name="param2">two</param>
        ...
        <param name="paramN">nth-number</param>
    </interceptor-ref>
...
</action>

动作参数按以下方式定义,动作参数和拦截器参数应单独访问。

<action name="theAction" ...>
...
    <param name="param1">one</param>
    <param name="param2">two</param>
    ...
    <param name="paramN">nth-number</param>
    ....
    <interceptor-ref name="theInterceptor">
        <param name="param1">one</param>
        <param name="param2">two</param>
        ...
        <param name="paramN">nth-number</param>
    </interceptor-ref>
...
</action>

请注意,我不想将拦截器中的参数字段声明为

//all fields with their getters and setters
private String param1;
private String param2;
...
private String paramN;

在Dev Blanked's asnwer之后,我实施了他的技术。它没有用,所以我在这里分享我的代码。我正在使用Struts 2.3.1.2。

  • ASM-3.3.jar
  • ASM-公地3.3.jar
  • ASM-树-3.3.jar
  • 公地文件上传-1.2.2.jar
  • 公地-IO-2.0.1.jar
  • 公地琅2.5.jar
  • 的freemarker-2.3.18.jar
  • 了Javassist-3.11.0.GA.jar
  • OGNL-3.0.4.jar
  • struts2的核 - 2.3.1.2.jar
  • XWork的核 - 2.3.1.2.jar

struts.xml中

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
    <constant name="struts.devMode" value="true" />

    <package name="the-base" namespace="/" extends="struts-default" abstract="true">

        <interceptors>
            <interceptor name="header" class="demo.interceptors.HttpHeaderInterceptor"></interceptor>

        <interceptor-stack name="theStack">
            <interceptor-ref name="defaultStack"></interceptor-ref>
                <interceptor-ref name="header"></interceptor-ref>
            </interceptor-stack>
        </interceptors>

        <default-interceptor-ref name="theStack"></default-interceptor-ref>

    </package>

    <package name="the-module" extends="the-base">
        <action name="theAction">
            <result>/the-action.jsp</result>
            <interceptor-ref name="theStack">
                <param name="header.Cache-control">no-store,no-cache</param>
                <param name="header.Pragma">no-cache</param>
                <param name="header.Expires">-1</param>
                <param name="header.arbitrary">true</param>
            </interceptor-ref>
        </action>
    </package>
</struts>

拦截

package demo.interceptors;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.StrutsStatics;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

public class HttpHeaderInterceptor extends AbstractInterceptor {

    private final Map<String, String> interceptorConfigs = new HashMap<String, String>();

    @Override
    public String intercept(ActionInvocation invocation) throws Exception {
        System.out.println("Calling 'intercept' method.");
        HttpServletResponse response = (HttpServletResponse) invocation.getInvocationContext().get(StrutsStatics.HTTP_RESPONSE);

        for(Entry<String, String> entry: interceptorConfigs.entrySet()) {
            String header = entry.getKey();
            String value = entry.getValue();
            System.out.printf("Adding header: %s=%s\n",header,value);
            response.setHeader(header, value);
        }

        return invocation.invoke();
    }

    public Map<String, String> getInterceptorConfigs() {
        System.out.println("calling method 'getInterceptorConfigs'");
        return interceptorConfigs;
    }

    public void addInterceptorConfig(final String configName, final String configValue) {
        System.out.printf("Calling method 'addInterceptorConfig' with params configName = %s, configValue=%.\n",configName, configValue);
        interceptorConfigs.put(configName, configValue);
    }

}
点击theAction

控制台输出

Calling 'intercept' method. 

2 个答案:

答案 0 :(得分:2)

在您的自定义拦截器中,您可以定义如下的地图

private final Map<String, String> interceptorConfigs = new HashMap<String, String>();

public Map<String, String> getInterceptorConfigs() {
    return interceptorConfigs;
}


public void addInterceptorConfig(final String configName, final String configValue) {
    interceptorConfigs.put(configName, configValue);
}

然后在你的动作映射中你可以传递如下的参数..这些将存储在拦截器的地图中

    <action name="yourAction" class="your.actionClass">
        <result name="success">some.jsp</result>
        <interceptor-ref name="defaultStack">
            <param name="yourInterceptor.interceptorConfigs.key">value</param>
            <param name="yourInterceptor.interceptorConfigs.aParamName">paramValue</param>            </interceptor-ref>
    </action>

“yourInterceptor”指的是在将拦截器添加到struts.xml时给出的拦截器的名称。当配置如上所述'interceptorConfigs'时,拦截器内的地图将具有键/值对。

如果您想让这些可用于您的操作,您只需将地图设置为ActionContext中的上下文变量即可。然后可以在操作中检索它。

答案 1 :(得分:1)

要简短,我会说 no ,如果你在interceptor-ref元素中定义了拦截器参数,就无法获得拦截器参数。在构建期间设置参数并将其应用于拦截器。但是,如果您将参数添加到interceptor元素,如

<interceptor name="theInterceptor" class="com.struts.interceptor.TheInterceptor">
  <param name="param1">one</param>
  <param name="param2">two</param>
</interceptor>

你可以动态检索它们

PackageConfig packageConfig = Dispatcher.getInstance().getConfigurationManager().getConfiguration().getPackageConfig("default");
Map<String, Object> interceptorConfigs = packageConfig.getInterceptorConfigs();
InterceptorConfig interceptorConfig =  (InterceptorConfig)interceptorConfigs.get("theInterceptor");
Map<String, String> params = interceptorConfig.getParams();  

如果您不想在拦截器上定义属性来保存值,那么OGNL将不会设置值但会尝试,所以我没有看到不定义这些属性的原因,xml配置标记为无效如果你的拦截器bean不包含这些属性,那么构建器可能会抛出异常。因此,不定义params的属性我不推荐。