Struts2 OGNL - 请求参数提交顺序
我正在使用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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,我假设您确实有一个
empId
字段的设置器(您没有显示一个),并且您的问题是参数的顺序设置是任意的。ParametersInterceptor
有一个选项可以强制它首先设置顶级属性。您可以通过自定义拦截器堆栈来启用它,以使用ordered
属性集定义参数拦截器。然后,在您的操作类中,将
setEmpId
方法更改为:作为 setter 方法的替代方法,您还可以为
Employee
类创建一个类型转换器,然后更改您的表格至: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 theordered
property set.Then, in your action class, change the
setEmpId
method to:As an alternative to the setter approach, you could also create a type converter for the
Employee
class and then change your form to:我不确定我是否理解,所以要明确的是,您想调用 getEmployee() 并且您不知道如何在该方法中获取员工 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看起来大致像(未经测试):
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:
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):