I created a single jsp form which wants to send data to two table. It means I created two model class, so the form should refer those two model class. I tried, but failed. How to get two modelAttribute from one jsp page and assign to one controller. Thanks in advance.
`@Entity
@Table(name = "survey")
public class Survey {
// all anotations and getters and setters are omitted
private int surveyId;
private String surveyName;
}
@Entity
@Table(name = "preferred")
public class Survey {
// all anotations and getters and setters are omitted
private int preferredId;
private String preferredClass;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name="surveyId")
private Survey survey;
}
jsp form
<spring:url value="/survey/save" var="saveURL"></spring:url>
<form:form action="${saveURL}" method="POST" modelAttribute="surveyForm">
//Survey model class
<form:hidden path="surveyId" />
<form:input path="surveyName" />
//preferredClass model class
<form:input path="preferredClass">
<button type="submit" >Submit</button>
</form:form>
Controller
@RequestMapping(value = "/save", method = RequestMethod.POST)
//here I need to get ModelAttribuete of Survey and preferredClass
public ModelAndView saveSurvey( @ModelAttribute("surveyForm") Survey survey ,.........// how to get attributes of preferredClass ) {
surveyService.saveOrUpdate(survey);
return new ModelAndView("redirect:/survey/list");
}`