我是Java的新手,所以这个问题看起来很简单。我有一个类似的模型:
@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....
}
你看,在网站类中,我有一个Publisher类型的对象:
@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...
}
好的,现在我有一个表单来提交网站类型的整个对象。我刚创建了一个下拉列表,让用户选择发布者: 表格:
<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>
你看,我设置了表单的路径:select tag为“publisher”,itemValue设置为“publisherId”。发布时,它会将publisherId(长类型值)发布到已发布对象的发布者属性。验证将失败,因为它需要Publisher类型,而不是长类型。
我的问题:我如何以正确的方式发布Website.Publisher.publisherId?
更新
我在控制器中添加操作:
@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";
}
我已将选择标记的路径从publisher
更改为publisher.publisherId
,如下所示,来自Shantaram Tupe,但无法将表单生成为html。我从publisher
更改为publisher.publisherName
,一切顺利,表单可以在html中查看,值在publisher.publisherName
中发回服务器。我怎样才能将其发回publisher.publisherId
?我的publisherId字段的配置看起来有问题吗?
更新2
我的问题是仅生成HTML表单。我尝试在浏览器上编辑生成的html表单,如:
<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
生成表单?
最后更新
最后,我发现了它。它适用于PublisherName但不适用于PublisherId,因为PublisherName是字符串类型。我使用的select标记中的转储项(<form:option value="NONE" label="--- Select ---" />
)的值为NONE
- 不能是长类型值。将其更改为0
并成功生成表单。
答案 0 :(得分:1)
只需将<form:select>
代码中的路径从 publisher
更改为 publisher.publisherId
我身边的其他一些事情:
您无需使用@Column(name="websiteId", nullable=false, unique=true)
@Id
进行注释,因此绝不会 null ,默认情况下 uniqe 您无需在@JoinColumn
方使用@OneToMany
方使用@OneToMany(mappedBy="publisher")
使用 mappedBy 属性,例如displayName