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

2022-01-16 00:00:00 regex java jsp struts2 ognl

该问题与 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.

相关文章