簡體   English   中英

在Struts 2中獲取攔截器參數

[英]Getting Interceptor Parameters in Struts 2

我有以下動作映射

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

我可以在Interceptor中使用以下行獲取參數映射

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-的commons-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. 

在您的自定義攔截器中,您可以定義如下所示的地圖

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的上下文變量即可。 然后可以在操作中檢索它。

要短,我會說沒有 ,你不能攔截參數,如果你在定義他們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的屬性我不推薦。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM