与 Spring MVC JSP 页面绑定混淆
我一直无法解决我的绑定问题。
我有一个类 Person
Class Person {
Private fname;
private lname;
public Address address;
另一个类
class Address {
private street;
private suburb;
}
现在控制器中的
model.addAttribute("personAttribute", new Person());
我在 jsp 页面中有
<form:form modelAttribute="personAttribute" method="POST" action="${saveUrl}">
<td><form:input path="firstName"/></td>
Uptill 这个 evrrythng 是好的,但是当我添加
<td><form:input path="street"/></td>
然后就成为问题。我不知道如何将 Adress 对象与 person 添加。
我的意思是我必须做什么
model.addAttribute("personAttribute", new Person(new Address()));
or
model.addAttribute("addressAttribute", new Address());
or
td><form:input path="person.address.street"/></td>
I have not been able to solve my binding problem.
I have one class Person
Class Person {
Private fname;
private lname;
public Address address;
Now one other class
class Address {
private street;
private suburb;
}
in controller i have
model.addAttribute("personAttribute", new Person());
IN jsp page i have
<form:form modelAttribute="personAttribute" method="POST" action="${saveUrl}">
<td><form:input path="firstName"/></td>
Uptill this evrrythng is ok but when i add
<td><form:input path="street"/></td>
Then becomes the problem . i don't know how can i add Adress object with person.
i mean do i have to do
model.addAttribute("personAttribute", new Person(new Address()));
or
model.addAttribute("addressAttribute", new Address());
or
td><form:input path="person.address.street"/></td>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你必须像这样绑定路径:
这是因为你在绑定中的
Person
对象的范围内。you have to bind path like that :
It's because you are in scope of
Person
object in binding.以下代码是正确的:
这将用空属性(包括空地址)初始化 Person。
如果要确保地址不为空,则必须传递包含值的 person 实例。例如
,通常您可能希望使用从数据库或数据源检索的值来填充人员对象。
The following code is correct:
This will initialize the Person with empty attributes, including an empty Address.
If you want to make sure that the address is not empty, you have to pass a person instance that contains a value. For example
Normally you may want to populate the person object with a value retrieved from a database or data source.