通过Struts 2中的ModelDriven将参数传递给操作 [英] Passing parameters to action through ModelDriven in Struts 2

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

问题描述

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

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

在行动执行方法中我们得到了参数 name 应该由 params 拦截器填充,并初始化model属性以在结果中显示它。但问题是参数未填充。如何获取参数 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?

推荐答案

名称是模型的属性,也是动作类的属性。 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 之类的名称时,它会从值堆栈的顶部搜索到属性访问器的堆栈。第一个找到的访问者将被执行。因此,model属性具有优先级,因为模型位于值堆栈之上。

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 value stack 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]。名称。但是,默认模式 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还会检查模式接受的参数和此正则表达式模式允许匹配参数和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将参数传递给操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆