如何在 Spring 中使用 SimpleFormController 将多个表单输入合并到一个对象中?
我有一个名为 Person 的对象,它具有以下属性:
int id;
Name name;
String address;
Date birthday;
String email;
String note;
Name 类具有以下属性:
String firstName;
String middleName;
String lastName;
在我的表单中,我有这些输入字段:
<tr>
<td>First Name:</td>
<td><form:input path="firstName" /></td>
</tr>
<tr>
<td>Middle Name:</td>
<td><form:input path="middleName" /></td>
</tr>
<tr>
<td>Last Name:</td>
<td><form:input path="lastName" /></td>
</tr>
我如何能够检索名称输入字段的值并将其转换为 Name 对象在将其传递给将由 SimpleFormController 创建的 Person 对象之前?我很确定我需要使用 initBinder() 方法,但我不知道如何开始。
我正在使用 Spring 3.0,是的,我知道 SimpleFormController 已经被弃用,但我仍然打算使用它。
I have an object called Person that has the following properties:
int id;
Name name;
String address;
Date birthday;
String email;
String note;
The Name class has these properties:
String firstName;
String middleName;
String lastName;
In my form, I have these input fields:
<tr>
<td>First Name:</td>
<td><form:input path="firstName" /></td>
</tr>
<tr>
<td>Middle Name:</td>
<td><form:input path="middleName" /></td>
</tr>
<tr>
<td>Last Name:</td>
<td><form:input path="lastName" /></td>
</tr>
How will I be able to retrieve the value of the name input fields and turn it into a Name object before passing it to the Person object that will be created by the SimpleFormController? I'm pretty sure I need to use the initBinder() method, but I don't know how to start.
I'm using Spring 3.0, and yes, I know that SimpleFormController is deprecated already, but I still intend to use it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你需要
在你的jsp中 写:并且需要重写 formBackingObject 方法,以便它返回一个空的 Person 对象,并引用一个空的名称对象。
就这样,您不需要编写自己的绑定。
You need to write:
in you jsp. And need to override the formBackingObject Method, so that it retuns an empty Person object with an reference to an emtpy name object
Thats all, you do not need to write your own binding.