在提交Spring MVC上绑定子对象 [英] Binding child object on submit spring mvc
问题描述
我是Java的新手,所以这个问题看起来很简单.我有一个像这样的模型:
I'm newbie in Java so this question looks so simple. I have a model like:
@Entity(name="website")
public class Website {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="websiteId", nullable=false, unique=true)
private long websiteId;
@ManyToOne
@JoinColumn(name = "publisherId")
private Publisher publisher;
public Website() {
}
//... all getter and setter....
}
您看到在Website类中,我有一个Publisher类型的对象:
You see, inside Website class, i have an object of Publisher type:
@Entity(name="publisher")
public class Publisher {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="publisherId", nullable=false, unique=true)
private long publisherId;
private String publisherName;
@OneToMany(fetch=FetchType.EAGER, cascade = CascadeType.ALL)
@JoinColumn(name="publisherId")
private List<Website> listWebsite;
public Publisher() {
}
//...all getter and setter...
}
好的,现在我有一个表单来提交网站类型的整个对象.我刚刚创建了一个下拉列表,并允许用户选择发布者: 形式:
OK, now i have a form to submit whole object of Website type. I just created a dropdownlist and let user select publisher: The form:
<form:form action="/LineJavaTest1/website/add" commandName="websiteForm" method="post">
<form:input path="websiteName" size="30" placeholder="Website Name"/>
<form:input path="websiteUrl" size="30" placeholder="Website Url"/>
<form:select path="publisher" multiple="false" size="1">
<%--<form:options itemValue="publisherId" itemLabel="publisherName"/>--%>
<form:option value="NONE" label="--- Select ---" />
<form:options items="${publishers}" itemValue="publisherId" itemLabel="publisherName"/>
</form:select>
<form:hidden path="websiteId" size="30" placeholder="Website Id"/>
<input type="submit" class="btn btn-default" value="Save" />
</form:form>
您看到,我将form:select标记的路径设置为"publisher",itemValue设置为"publisherId".发布时,它将发布者ID(长型值)发布到发布对象的发布者属性中.验证将失败,因为它需要Publisher类型,而不是long类型.
You see, i set the path for the form:select tag as "publisher", the itemValue is set to "publisherId". When posting, it post the publisherId (a long type value) to the publisher property of posted object. The validation will be failed because it required Publisher type, not long type.
我的问题:如何以正确的方式发布Website.Publisher.publisherId?
My question: How do i posting the Website.Publisher.publisherId in the right way?
更新:
我在控制器中的添加操作:
My add actions in controller:
@RequestMapping(value = "/add", method = RequestMethod.GET)
public String showForm(ModelMap mm, @ModelAttribute("websiteForm") Website websiteForm) {
mm.put("websiteForm", new Website());
mm.put("publishers", publisherService.getAll());
return "website/add";
}
@RequestMapping(value = "/add", method = RequestMethod.POST)
public String add(@ModelAttribute("websiteForm") Website websiteForm, BindingResult result, ModelMap mm) {
websiteValidator.validate(websiteForm, result);
if (result.hasErrors()) {
return "error";
}
if (websiteForm.getWebsiteId()>0) {
websiteService.edit(websiteForm);
}else
{
websiteService.add(websiteForm);
}
return "redirect:index";
}
如下所示,我从Shantaram Tupe建议将选择标签的路径从publisher
更改为publisher.publisherId
,但是无法将表单生成为html.我从publisher
更改为publisher.publisherName
,一切看起来都很好,可以用html查看表单,将值发布回publisher.publisherName
中的服务器.我怎样才能将其重新发布到publisher.publisherId
中?我的PublisherId字段的配置看起来有问题吗?
I have changed the path of select tag from publisher
to publisher.publisherId
as below suggest from Shantaram Tupe but the form cannot be generated to html. I changed from publisher
to publisher.publisherName
and everything looks well, the form can be viewed in html, the value post back to server in publisher.publisherName
. How can i just post it back in publisher.publisherId
? It looks something's wrong in configuration for my publisherId field?
更新2
我的问题是仅生成HTML表单.我试过在浏览器上通过类似以下方式编辑生成的html表单:
My issue is generating HTML form only. I have tried editing the generated html form on browser from something like:
<form.....>
<select id="publisher.publisherName" name="publisher.publisherName" size="1">......</select>
</form>
类似于:
<form.....>
<select id="publisher.publisherId" name="publisher.publisherId" size="1">......</select>
</form>
一切正常.现在,如何使用publisher.publisherId
生成表单?
And everything work well. Now, How can i generate the form with publisher.publisherId
?
最后更新
最后,我找到了.它与PublisherName一起使用,但不适用于PublisherId,因为PublisherName是字符串类型.我使用的选择标记(<form:option value="NONE" label="--- Select ---" />
)中的转储项的值为NONE
-不能为长型值.将其更改为0
并成功生成了表单.
Finally, i found it. It works with PublisherName but not PublisherId because PublisherName is string type. The dump item in select tag i used (<form:option value="NONE" label="--- Select ---" />
) has the value NONE
- that cannot be a long type value. Changed it to 0
and the form generated successfully.
推荐答案
只需将<form:select>
标记中的路径从 publisher
更改为 publisher.publisherId
simply change path in <form:select>
tag from publisher
to publisher.publisherId
我这边还有其他事情:
-
您不需要使用
@Column(name="websiteId", nullable=false, unique=true)
- 因为数据库中的列名与实体类中的字段名相同
- 由于使用
@Id
进行了注释,因此永远不会 null ,并且默认情况下不会 uniqe
- Since You have column name in database same as field name in Entity class
- Since it is Annotated with
@Id
it will never null and by default uniqe
您无需在两端都使用@JoinColumn
,在@OneToMany
端使用 mappedBy 属性,例如@OneToMany(mappedBy="publisher")
You don't need to use @JoinColumn
at both side, at @OneToMany
side use mappedBy attribute e.g. @OneToMany(mappedBy="publisher")
这篇关于在提交Spring MVC上绑定子对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!