Obtenir de l'Intercepteur Paramètres dans Struts 2

J'ai de suite à l'action de la cartographie

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

Je peux obtenir le paramètre de la carte en utilisant la ligne suivante dans Intercepteur

Map<String, Object> params = ActionContext.getContext().getParameters();

Comme ci-dessus, est-il possible de l'intercepteur paramètres tels que définis dans la suite de la cartographie.

<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>

Et les paramètres de l'action sont définis de la manière suivante, les paramètres de l'action et de l'intercepteur paramètres doivent être accessibles séparément.

<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>

Veuillez noter que je ne veux pas déclarer les champs de paramètre dans mon intercepteur

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

Après Dev Effacé de répondre, j'ai mis en place sa technique. Il n'a pas de travail donc je suis le partage de mon code ici. Je suis en utilisant Struts 2.3.1.2.

Bibliothèques

  • asm-3.3.jar
  • asm-commons-3.3.jar
  • asm-tree-3.3.jar
  • commons-fileupload-1.2.2.jar
  • commons-io-2.0.1.jar
  • commons-lang-2.5.jar
  • freemarker-2.3.18.jar
  • javassist-3.11.0.GA.jar
  • ognl-3.0.4.jar
  • struts2-core-2.3.1.2.jar
  • xwork-core-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>

Intercepteur

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

Sortie de la Console quand theAction est frappé.

Calling 'intercept' method. 
Peut-être avec la liste de paramètre à l'intérieur de l'intercepteur. Quelque chose de similaire à cette question: stackoverflow.com/q/14474492/1700321.
Il n'est pas clair ce que vous essayez d'atteindre 😐
Ligios, j'ai édité ma question. Cependant, mon objectif est d'obtenir les paramètres de la carte définie à l'intérieur d' <interceptor-ref> de la balise body.
M, Merci pour le lien. Mais dans ce cas, n'importe quel nom de paramètre peut être défini dans la configuration.

OriginalL'auteur Bilal Mirza | 2013-05-08