在Struts 2中获取拦截器参数

我有以下动作映射

 ... one two ... nth-number ...  

我可以在Interceptor中使用以下行获取参数映射

 Map params = ActionContext.getContext().getParameters(); 

如上所述, 有没有办法获得下面的映射中定义的拦截器参数

  ...  one two ... nth-number  ...  

并且动作参数以下面的方式定义,动作参数和拦截器参数应该可以单独访问。

  ... one two ... nth-number ....  one two ... nth-number  ...  

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

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

                 /the-action.jsp  no-store,no-cache no-cache -1 true     

拦截器

 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 interceptorConfigs = new HashMap(); @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 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 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); } } 

击中动作时的控制台输出

 Calling 'intercept' method. 

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

 private final Map interceptorConfigs = new HashMap(); public Map getInterceptorConfigs() { return interceptorConfigs; } public void addInterceptorConfig(final String configName, final String configValue) { interceptorConfigs.put(configName, configValue); } 

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

   some.jsp  value paramValue   

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

如果要使这些可用于您的操作,您只需将映射设置为ActionContext的上下文变量即可。 然后可以在操作内部检索它。

简而言之,我会说不,如果在interceptor-ref元素中定义它们,则无法获取拦截器参数。 在构建期间设置参数并将其应用于拦截器。 但是,如果你把参数放到interceptor元素上就好了

  one two  

你可以动态检索它们

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

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