java 在 Struts 2 中获取拦截器参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16441028/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting Interceptor Parameters in Struts 2
提问by Bilal Mirza
I have 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 get parameter map using following line in Interceptor
我可以在 Interceptor 中使用以下行获取参数映射
Map<String, Object> params = ActionContext.getContext().getParameters();
Just as above, is there any way to get interceptor parametersas defined in 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>
And action parameters are defined in following way, 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>
Please note that I don't want to declare parameter fields in my interceptor as
请注意,我不想在拦截器中将参数字段声明为
//all fields with their getters and setters
private String param1;
private String param2;
...
private String paramN;
After Dev Blanked's asnwer, I implemented his technique. It did not work so I am sharing my code here. I am using Struts 2.3.1.2.
在 Dev Blanked 的回答之后,我实施了他的技术。它没有用,所以我在这里分享我的代码。我正在使用 Struts 2.3.1.2。
Libraries
图书馆
- 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
- 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
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 Outputwhen theAction
is hit.
控制台输出时theAction
被击中。
Calling 'intercept' method.
采纳答案by Dev Blanked
In your custom interceptor you can define a map like below
在您的自定义拦截器中,您可以定义如下地图
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);
}
Then in your action mappings you can pass in parameters like below .. these will be stored in the map of the interceptor
然后在您的动作映射中,您可以传入如下参数..这些将存储在拦截器的映射中
<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" refers to the name of the interceptor you have given when adding your interceptor to the struts.xml. When configured like above 'interceptorConfigs' map inside the interceptor will have , key/value pairs.
“yourInterceptor”指的是你在将拦截器添加到 struts.xml 时给出的拦截器的名称。当像上面那样配置'interceptorConfigs' 时,拦截器内的映射将具有 , 键/值对。
If you want to make these available to your action, you can just set the map as a context variable in the ActionContext
. This can then be retrieved inside the action.
如果您想让这些对您的操作可用,您只需将地图设置为ActionContext
. 然后可以在操作中检索它。
回答by Roman C
To be short I'll say no, you can't get interceptor parameters if you defined them in the interceptor-ref
element. The parameters are set and applied to the interceptor during build time. However, if you put parameters to the interceptor
element like
要短,我会说没有,如果你在定义他们,你无法获得拦截器参数interceptor-ref
元素。参数在构建期间设置并应用于拦截器。但是,如果您将参数放入interceptor
元素中,例如
<interceptor name="theInterceptor" class="com.struts.interceptor.TheInterceptor">
<param name="param1">one</param>
<param name="param2">two</param>
</interceptor>
you could retrieve them on the fly
你可以随时取回它们
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();
If you don't want to define properties on the interceptor to hold the values then OGNL will not set the values but will try, so I don't see the reasons to not to define these properties, the xml configuration marked invalid if your interceptor bean doesn't contain these properties and builder might be throw an exception in this case. So, not defining properties for params I'm not recommending.
如果您不想在拦截器上定义属性来保存值,那么 OGNL 将不会设置值但会尝试,所以我看不出不定义这些属性的原因,如果您的拦截器将 xml 配置标记为无效bean 不包含这些属性,在这种情况下构建器可能会抛出异常。因此,我不推荐不为 params 定义属性。