Java – get interceptor parameters in struts 2

I have the following action mapping

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

I can use the following lines in the interceptor to get the parameter mapping

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

As mentioned above, is there any way to obtain the interceptor parameters defined in the following mapping

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

The action parameters are defined in the following ways. The action parameters and interceptor parameters should be accessible separately

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

Note that I don't want to declare parameter fields in the interceptor

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

After dev blanked's asnwer, I implemented his technology It doesn't work, so I share my code here I am using struts 2.3 1.2.

library

> 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

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

Interceptor

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

}

Console output when triggering action

Calling 'intercept' method.

Solution

In your custom interceptor, you can define the map shown below

private final Map<String,String>();

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


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

Then in your action mapping, you can pass the following parameters These will be stored in the interceptor's map

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

"Your interceptor" means adding an interceptor to struts The name of the interceptor given when XML When 'interceptorconfigures' is configured as described above, the map in the interceptor will have key / value pairs

If you want to make these available for your actions, you just need to set the mapping to the context variable in actioncontext It can then be retrieved in the operation

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>