【发布时间】:2018-01-20 10:50:04
【问题描述】:
我是 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....
}
你看,在 Website 类中,我有一个 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 标记设置为“publisher”,itemValue 设置为“publisherId”。发布时,它将publisherId(长类型值)发布到发布对象的发布者属性中。验证将失败,因为它需要 Publisher 类型,而不是 long 类型。
我的问题:如何以正确的方式发布 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";
}
我已将 select 标记的路径从 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 是字符串类型。我使用的选择标记中的转储项 (<form:option value="NONE" label="--- Select ---" />) 具有值 NONE - 它不能是长类型值。改成0,表单生成成功。
【问题讨论】:
-
publisherId除了NONE之外还有哪些可能的值?如果都是long类型,请考虑将NONE更改为long类型的其他值。 .
标签: java spring jsp spring-mvc