<tfoot id='Lg3NB'></tfoot>

      <small id='Lg3NB'></small><noframes id='Lg3NB'>

        <bdo id='Lg3NB'></bdo><ul id='Lg3NB'></ul>
    1. <i id='Lg3NB'><tr id='Lg3NB'><dt id='Lg3NB'><q id='Lg3NB'><span id='Lg3NB'><b id='Lg3NB'><form id='Lg3NB'><ins id='Lg3NB'></ins><ul id='Lg3NB'></ul><sub id='Lg3NB'></sub></form><legend id='Lg3NB'></legend><bdo id='Lg3NB'><pre id='Lg3NB'><center id='Lg3NB'></center></pre></bdo></b><th id='Lg3NB'></th></span></q></dt></tr></i><div id='Lg3NB'><tfoot id='Lg3NB'></tfoot><dl id='Lg3NB'><fieldset id='Lg3NB'></fieldset></dl></div>

        <legend id='Lg3NB'><style id='Lg3NB'><dir id='Lg3NB'><q id='Lg3NB'></q></dir></style></legend>

        在 Struts 2 中获取拦截器参数

        Getting Interceptor Parameters in Struts 2(在 Struts 2 中获取拦截器参数)

            1. <i id='LUiT1'><tr id='LUiT1'><dt id='LUiT1'><q id='LUiT1'><span id='LUiT1'><b id='LUiT1'><form id='LUiT1'><ins id='LUiT1'></ins><ul id='LUiT1'></ul><sub id='LUiT1'></sub></form><legend id='LUiT1'></legend><bdo id='LUiT1'><pre id='LUiT1'><center id='LUiT1'></center></pre></bdo></b><th id='LUiT1'></th></span></q></dt></tr></i><div id='LUiT1'><tfoot id='LUiT1'></tfoot><dl id='LUiT1'><fieldset id='LUiT1'></fieldset></dl></div>
                  <tbody id='LUiT1'></tbody>

                • <bdo id='LUiT1'></bdo><ul id='LUiT1'></ul>
                • <small id='LUiT1'></small><noframes id='LUiT1'>

                  <legend id='LUiT1'><style id='LUiT1'><dir id='LUiT1'><q id='LUiT1'></q></dir></style></legend>

                  <tfoot id='LUiT1'></tfoot>
                  本文介绍了在 Struts 2 中获取拦截器参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有以下动作映射

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

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

                  I can get parameter map using following line in Interceptor

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

                  如上,有没有办法获取拦截器参数,如下映射中定义的那样.

                  Just as above, is there any way to get interceptor parameters as 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;
                  

                  <小时>

                  在 Dev Blanked 的回答之后,我实施了他的技术.它没有用,所以我在这里分享我的代码.我正在使用 Struts 2.3.1.2.


                  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.

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

                  拦截器

                  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
                  ",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=%.
                  ",configName, configValue);
                          interceptorConfigs.put(configName, configValue);
                      }
                  
                  }
                  

                  控制台输出,当 theAction 被点击时.

                  Calling 'intercept' method. 
                  

                  推荐答案

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

                  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" 是指你在将拦截器添加到 struts.xml 时给出的拦截器的名称.当像上面那样配置'interceptorConfigs'时,拦截器内的映射将有,键/值对.

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

                  如果您想让这些对您的操作可用,您只需将地图设置为 ActionContext 中的上下文变量.然后可以在操作中检索它.

                  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.

                  这篇关于在 Struts 2 中获取拦截器参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Compiling C++ for the JVM(为 JVM 编译 C++)
                  Compile to java bytecode (without using Java)(编译成java字节码(不使用Java))
                  How to drive C#, C++ or Java compiler to compute 1+2+3+...+1000 at compile time?(如何在编译时驱动 C#、C++ 或 Java 编译器计算 1+2+3+...+1000?)
                  Java ClassLoader: load same class twice(Java ClassLoader:两次加载相同的类)
                  How to debug .class files in ECLIPSE?(如何在 ECLIPSE 中调试 .class 文件?)
                  Java quot;The blank final field may not have been initializedquot; Anonymous Interface vs Lambda Expression(Java“可能尚未初始化空白的最终字段匿名接口与 Lambda 表达式)
                    <bdo id='Pc6s4'></bdo><ul id='Pc6s4'></ul>

                      <tbody id='Pc6s4'></tbody>

                    1. <i id='Pc6s4'><tr id='Pc6s4'><dt id='Pc6s4'><q id='Pc6s4'><span id='Pc6s4'><b id='Pc6s4'><form id='Pc6s4'><ins id='Pc6s4'></ins><ul id='Pc6s4'></ul><sub id='Pc6s4'></sub></form><legend id='Pc6s4'></legend><bdo id='Pc6s4'><pre id='Pc6s4'><center id='Pc6s4'></center></pre></bdo></b><th id='Pc6s4'></th></span></q></dt></tr></i><div id='Pc6s4'><tfoot id='Pc6s4'></tfoot><dl id='Pc6s4'><fieldset id='Pc6s4'></fieldset></dl></div>

                        <small id='Pc6s4'></small><noframes id='Pc6s4'>

                        <legend id='Pc6s4'><style id='Pc6s4'><dir id='Pc6s4'><q id='Pc6s4'></q></dir></style></legend>
                        • <tfoot id='Pc6s4'></tfoot>