• <bdo id='x3MQi'></bdo><ul id='x3MQi'></ul>

  • <legend id='x3MQi'><style id='x3MQi'><dir id='x3MQi'><q id='x3MQi'></q></dir></style></legend>

      1. <small id='x3MQi'></small><noframes id='x3MQi'>

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

        <tfoot id='x3MQi'></tfoot>

        通过 Struts 2 中的 ModelDriven 将参数传递给动作

        时间:2023-09-25

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

          <bdo id='UEVOO'></bdo><ul id='UEVOO'></ul>
          <tfoot id='UEVOO'></tfoot>

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

              1. <legend id='UEVOO'><style id='UEVOO'><dir id='UEVOO'><q id='UEVOO'></q></dir></style></legend>

                1. 本文介绍了通过 Struts 2 中的 ModelDriven 将参数传递给动作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  该问题与 ModelDriven 和 Struts 2.3.16 有关.由于 params 拦截器的行为更改为访问传递给操作的参数,因此需要配置 acceptParamNames 列表以与 ModelDriven 操作一起使用.如果 acceptParamNames 列表为空,则默认通过默认模式接受参数.假设我们有一个

                  The issue is related to the ModelDriven and Struts 2.3.16. Since the behavior of the params interceptor changed to access parameters passed to the action requires to configure acceptParamNames list to use with ModelDriven action. If acceptParamNames list is empty, it works by default accepting params via default pattern. Suppose we have a

                  ModelDriven 操作:

                  ModelDriven action:

                  @Namespace("/modelDriven")
                  public class ModelDrivenAction extends ActionSupport implements ModelDriven {
                  
                    private Gangster model = new Gangster();
                  
                    private String name; //getter and setter
                  
                    public Object getModel() {
                      return model;
                    }
                  
                    @Actions({
                      @Action(value="modelDriven", results=@Result(location = "/modelDriven/modelDriven.jsp")),
                      @Action(value="modelDrivenResult", results=@Result(location = "/modelDriven/modelDrivenResult.jsp"))
                    })
                    public String execute() throws Exception {
                      model.setName(name);
                      return SUCCESS;
                    }
                  }
                  

                  型号:

                  public class Gangster {
                    private String name; //getter and setter
                  }
                  

                  modelDriven.jsp:

                  <s:form id="modelDrivenForm" action="modelDrivenResult" method="POST" namespace="/modelDriven">    
                      <s:textfield
                          label="Gangster Name"
                          name="[1].name"/>
                      <sj:submit cssClass="btn btn-primary" executeScripts="true" targets="div1"/>
                  </s:form>
                  
                  <div id="div1"/>
                  

                  modelDrivenResult.jsp:

                  <s:label
                      label="Gangster Name"
                      name="name"/><br/>
                  

                  在动作 execute 方法中,我们得到参数 name 应该由 params 拦截器填充并初始化模型属性以显示它结果.但问题是未填充参数.如何获取参数 name 被 params 拦截器填充,以便操作可以显示值?

                  In the action execute method we are getting parameter name which should be populated by the params interceptor and initializing the model property to display it in the result. But the problem is the parameter is not populated. How to get parameter name being populated by the params interceptor, so the action could display the value?

                  推荐答案

                  name是模型的属性,也是动作类的属性.modelDriven 拦截器模型位于值堆栈之上,因此在 JSP 中很容易使用.动作对象位于模型下方.因此,可以使用 [1] 前缀直接引用它.请参阅 OGNL 基础知识.

                  The name is the property of the model and also the property of the action class. The modelDriven interceptor pushes the model on top of the value stack, so it is easy to use it in JSP. The action object is below the model. So, it could be referenced directly using [1] prefix. See OGNL basics.

                  但如果模型和动作对象中没有重复的属性名称,则没有必要.当 OGNL 评估诸如 name 之类的名称时,它会从 valueStack 的顶部向下搜索属性访问器.第一个找到的访问器将被执行.因此,模型属性具有优先级,因为模型位于值堆栈的顶部.

                  But it's not necessary if there's no duplicate property names in the model and action object. When the name such as name is evaluated by OGNL it searches from the top of the valueStack to down the stack for the property accessor. The first found accessor will be executed. So, the model property has a priority because the model is on top of the value stack.

                  如果应该在操作上设置名称为 name 的属性,那么您可以直接将该属性命名为 [1].name.但是,params拦截器.但是,它是一个有效的 OGNL 表达式.因此,要让它通过拦截器,您需要将其添加到接受参数名称的模式中.就这样

                  If the property with the name name should be set on the action then you could directly name that property as [1].name. But, such parameter name is not accepted by default pattern of params interceptor. However, it is a valid OGNL expression. So, to get it pass through the interceptor you need to add it to a pattern of accepted parameter names. Like that

                  @Action(value="modelDrivenResult", results=@Result(location = "/modelDriven/modelDrivenResult.jsp"),
                    interceptorRefs = @InterceptorRef(value="defaultStack", params={
                      "params.acceptParamNames", "(\[\d+\]\.)*\w+((\.\w+)|(\[\d+\])|(\(\d+\))|(\['\w+'\])|(\('\w+'\)))*"
                    })
                  )
                  

                  这是因为 OGNL 还会检查接受参数的模式,并且此正则表达式模式允许匹配 params 和 OGNL 匹配器.

                  This is because OGNL also checks the pattern of accepted parameters and this regex pattern allows to match both params and OGNL matchers.

                  这篇关于通过 Struts 2 中的 ModelDriven 将参数传递给动作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                    <tbody id='9bvto'></tbody>

                2. <legend id='9bvto'><style id='9bvto'><dir id='9bvto'><q id='9bvto'></q></dir></style></legend>
                      <bdo id='9bvto'></bdo><ul id='9bvto'></ul>
                      <i id='9bvto'><tr id='9bvto'><dt id='9bvto'><q id='9bvto'><span id='9bvto'><b id='9bvto'><form id='9bvto'><ins id='9bvto'></ins><ul id='9bvto'></ul><sub id='9bvto'></sub></form><legend id='9bvto'></legend><bdo id='9bvto'><pre id='9bvto'><center id='9bvto'></center></pre></bdo></b><th id='9bvto'></th></span></q></dt></tr></i><div id='9bvto'><tfoot id='9bvto'></tfoot><dl id='9bvto'><fieldset id='9bvto'></fieldset></dl></div>

                      1. <small id='9bvto'></small><noframes id='9bvto'>

                      2. <tfoot id='9bvto'></tfoot>