I am using spring framework. This question mainly concerns about design and implementation.
In my project, I have to use many forms and most of them are different. What is the recommended way of implementing forms in spring. Using Model?
I want to use ajax for forms submissions. The forms in the project are really huge having 8-15 fields. Is it a good way to use ajax for such huge forms? If yes, how can I do it? Can I use model attribute?
A typical way to do this is to use form backing objects. For example, with a form like
<form>
<input type="text" name="username">
<input type="password" name="password">
<input type="text" name="email">
<input type="submit" name="submit">
</form>
You would create a DTO class
public class UserForm {
private String username;
private String password;
private String email;
public UserForm() {}
// getters and setters
}
Then your @Controller
handler method can map as
@RequestMapping(method = RequestMethod.POST)
public String handleFormSubmit(@ModelAttribute UserForm userForm /*, more */) {
/* logic and return */
}
And Spring will be able to bind the value from the name
attribute of input
elements to the fields of the class.
AJAX doesn't care about the size of your forms.