0

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.

1
  • Take a look at Hibernate and other ORM frameworks. Or take a look at JDBC (and after Spring data) on how to persist objects to database. Commented Mar 26, 2013 at 19:36

1 Answer 1

1

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" />
Sign up to request clarification or add additional context in comments.

3 Comments

how should i do that. what are the changes in here to be made.
can you comment on DTO. any advantage for using it rather than form bean.
A DTO allows communication between different systems. My suggestion could be considered a DTO for a particular communication (between the View and the Controller). A DTO might be a completely new object with different representation of your existing data. This would work, but would be very hard to maintain. If you add a field in one place, you would need to add it to the other object. You avoid that with encapsulation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.