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.... } 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> 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.
My question: How do i posting the Website.Publisher.publisherId in the right way?
UPDATE:
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"; } 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?
UPDATE 2
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> to something like:
<form.....> <select id="publisher.publisherId" name="publisher.publisherId" size="1">......</select> </form> And everything work well. Now, How can i generate the form with publisher.publisherId?
LAST UPDATE
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.
publisherId, other thanNONE?, if all are oflongtype, consider changingNONE, to something else tolongtype...