Reputation: 3975
I have something like
@RequestMapping("/showRegister")
public String showUserRegistrationForm(ModelMap modelMap) {
modelMap.addAttribute("user", new UserBean());
return "Register";
}
@RequestMapping("/RegisterUser")
public String registerUser(@ModelAttribute("user") UserBean userBean,
BindingResult result, ModelMap modelMap) {
System.out.println(userBean.getPassword());
return "Register";
}
in my code.
The above works perfectly. Now suppose I want to save the modal data from form into multiple tables each having its own POJO Class. So how would the code be so as to receive not just UserBean as modelattribute but also other classes. Will I have to create a new POJO containing data from both the classes or is there a other way around.
EDIT
I read about DTO. But doesn't it make a repetition of POJO's. Can't we use a mix of 2-3 POJO's instead.
Upvotes: 0
Views: 1312
Reputation: 4380
You could create a "form" bean, and add your UserBean and any other pojos to it as members. I actually prefer to do this, since it makes complex validation easier and more self contained.
public class MyFormBean {
private UserBean userBean;
private MyOtherBean otherBean;
// Add getters and setters as needed
}
Then your form needs to reference the correct path to drill down into your object. If you had something like:
<form:input path="name" />
You would change it to
<form:input path="userBean.name" />
Upvotes: 1