Struts2 OGNL - 请求参数提交顺序

发布于 2024-10-20 23:51:44 字数 660 浏览 9 评论 0原文

我正在使用jsp和struts2,并且有以下场景:

<s:form>
<s:hidden name="empId" value="123"/>
<s:textfield name="employee.name"/>
<s:submit action="save"/>
</s:form>

提交此表单时,OGNL表达式employee.name(相当于getEmployee().setName())在“save”方法之前执行。并且,“empId”的值在 getEmployee() 方法中不可用。 “empId”的值仅在“save”方法中可用。是否可以在 getEmployee() 中获取“empId”的值?

以下是我的 Action 类中的代码:

public String save() {
  //empId is available here
  return SUCCESS;
}

public Employee getEmployee(){
  if (employee == null){
    //empId is not available here
    employee = employeeService.get(empId);
  }
  return employee;
}

I am using jsp and struts2, and I have the following scenario:

<s:form>
<s:hidden name="empId" value="123"/>
<s:textfield name="employee.name"/>
<s:submit action="save"/>
</s:form>

When this form is submitted, the OGNL expression employee.name (equivalent to getEmployee().setName()) gets executed before "save" method. And, the value for "empId" is not available inside the method getEmployee(). The value for "empId" is available only inside the "save" method. Is it possible to get the value of "empId" inside getEmployee()?

Following is the code in my Action class:

public String save() {
  //empId is available here
  return SUCCESS;
}

public Employee getEmployee(){
  if (employee == null){
    //empId is not available here
    employee = employeeService.get(empId);
  }
  return employee;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

反目相谮 2024-10-27 23:51:44

首先,我假设您确实有一个empId字段的设置器(您没有显示一个),并且您的问题是参数的顺序设置是任意的。

ParametersInterceptor 有一个选项可以强制它首先设置顶级属性。您可以通过自定义拦截器堆栈来启用它,以使用 ordered 属性集定义参数拦截器。

<interceptor-ref name="params">
    <param name="ordered">true</param>
</interceptor-ref>

然后,在您的操作类中,将 setEmpId 方法更改为:

public void setEmpId(Integer empId) { // or whatever type it is
    this.empId = empId;
    employee = employeeService.get(empId);
}

作为 setter 方法的替代方法,您还可以为 Employee 类创建一个类型转换器,然后更改您的表格至:

<s:form>
    <s:hidden name="employee" value="123"/>
    <s:textfield name="employee.name"/>
    <s:submit action="save"/>
</s:form>

First, I assume that you do have a setter for the empId field (you didn't show one) and that your problem is that the order in which the parameters are being set is arbitrary.

There is an option for the ParametersInterceptor to force it to set top-level properties first. You can enable it by customizing your interceptor stack to define the parameters interceptor with the ordered property set.

<interceptor-ref name="params">
    <param name="ordered">true</param>
</interceptor-ref>

Then, in your action class, change the setEmpId method to:

public void setEmpId(Integer empId) { // or whatever type it is
    this.empId = empId;
    employee = employeeService.get(empId);
}

As an alternative to the setter approach, you could also create a type converter for the Employee class and then change your form to:

<s:form>
    <s:hidden name="employee" value="123"/>
    <s:textfield name="employee.name"/>
    <s:submit action="save"/>
</s:form>
始终不够爱げ你 2024-10-27 23:51:44

我不确定我是否理解,所以要明确的是,您想调用 getEmployee() 并且您不知道如何在该方法中获取员工 ID?

假设您有一张员工表。我们还假设该表提供以下详细信息:

  • 员工姓名
  • 员工 ID

假设每行中都有一个链接,单击该链接将转到该员工的员工详细信息屏幕。由于您刚刚打印了员工 ID,因此您还可以使用所需的 get 参数构造一个 html 锚元素,以便在调用该操作时您将获得所需的内容。 标签和 标签使这一切变得简单。

请参阅我的答案 Tiles2 Struts Switch Locale 了解如何使用 struts2 的示例锚标记和属性标记。尽管该示例使用静态属性,但只需将参数标记中的 value 属性替换为 id 变量。

请参阅 http://struts.apache.org/2.2.1.1/docs/a .html 了解更多详细信息。

编辑:我现在发现,在您进行编辑之前,我已经偏离了正轨。

我认为最简单的事情是创建一个 getEmployee(int id) 方法。然后你也可以去掉隐藏字段的值。
之后它应该是直截了当的......

你的jsp看起来大致像(未经测试):

<s:form>
   <s:textfield name="%{employee[123].name}"/>
   <s:submit action="save"/>
</s:form>

I'm not sure I understand so to be clear, you want to call getEmployee() and you don't know how to get the employee id in the method?

Lets say you have a table of employees. Lets also assume that the table provides the following details:

  • employee name
  • employee id

Lets assume that there is a link also in each row which when clicked will take you to an employee details screen for that employee. Since you have just printed the employee id you can also construct an html anchor element with the required get parameters so when the action is called you'll have what you need. The <s:a> tag along with the <s:param> tag make this easy.

See my answer here Tiles2 Struts Switch Locale for an example of how to use struts2 anchor tag and properties tag. Although that example makes use of static properties just replace the value attribute in the parameter tag with the id variable.

See http://struts.apache.org/2.2.1.1/docs/a.html for more details.

Edit: I see now I was way off before your edit.

I think the easiest thing would be to make a getEmployee(int id) method. Then you can get rid the the hidden field value too.
After that it should be straight forward...

Your jsp would look roughly like (not tested):

<s:form>
   <s:textfield name="%{employee[123].name}"/>
   <s:submit action="save"/>
</s:form>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文