Spring MVC Tutorial
Core Spring MVC
Spring MVC - Annotation
Spring MVC - Form Handling
Spring MVC with JSTL
Spring MVC with REST API
Spring MVC with Database
The @ModelAttribute
annotation in Spring MVC serves several purposes:
Automatic binding of form parameters to a bean. When a form is submitted, the form parameters can be automatically bound to a bean's properties, so that the controller can easily use the populated bean.
Setting default objects. The @ModelAttribute
method in a controller is invoked before any request handler methods. It can be used to add attributes to the model, which can then be accessed by the view layer or used for binding in request handler methods.
Reference data binding. It can be used to populate default data like dropdown values.
Let's go through a simple example:
Suppose you have a User
model class:
public class User { private String name; private String email; // Constructors, getters, setters, etc. }
In this controller, you'll see how @ModelAttribute
can be used in multiple ways:
@Controller @RequestMapping("/user") public class UserController { // This method will be called before any request-handler methods. // The returned User object will be added to the model under the attribute name "user". @ModelAttribute("user") public User setUpUserForm() { return new User(); } @GetMapping("/create") public String createUserForm() { return "userForm"; } @PostMapping("/save") public String saveUser(@ModelAttribute User user, Model model) { model.addAttribute("message", "User saved successfully with details: " + user.getName() + ", " + user.getEmail()); return "userResult"; } }
This view will create a form for capturing the user's information:
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %> <form:form action="/user/save" method="post" modelAttribute="user"> Name: <form:input path="name" /><br> Email: <form:input path="email" /><br> <input type="submit" value="Submit" /> </form:form>
This view will display the result:
<h2>${message}</h2>
The setUpUserForm()
method annotated with @ModelAttribute("user")
ensures that a default User
object is available in the model under the name "user". This method is called before any other request-handler methods.
The saveUser()
method takes the User
object as a parameter. When the form is submitted, the form fields will automatically be bound to the properties of this User
object, thanks to the @ModelAttribute
annotation.
Navigate to /user/create
, and you should see a form for entering the user's details. After entering the details and submitting, you should see a result page displaying the saved user details.
In summary, @ModelAttribute
is a powerful feature of Spring MVC that makes it easier to bind form data, pre-populate forms, and add reference data.
Spring MVC @ModelAttribute Example:
Description: This is a basic example demonstrating the usage of @ModelAttribute
in a Spring MVC controller to add an attribute to the model.
Code Snippet: (Controller)
// Controller class @Controller public class MyController { @ModelAttribute("welcomeMessage") public String addWelcomeMessage() { return "Welcome to Spring MVC!"; } @RequestMapping("/home") public String home() { return "home"; } }
@ModelAttribute Annotation Usage in Spring MVC:
Description: This example provides an overview of how to use the @ModelAttribute
annotation in Spring MVC controllers to add attributes to the model.
Code Snippet: (Controller)
// Controller class @Controller public class MyController { @ModelAttribute("message") public String addMessage() { return "Hello from @ModelAttribute!"; } @RequestMapping("/greet") public String greet() { return "greet"; } }
How to Use @ModelAttribute in Spring MVC Controller:
Description: This example demonstrates the usage of @ModelAttribute
in a Spring MVC controller to add model attributes.
Code Snippet: (Controller)
// Controller class @Controller public class MyController { @ModelAttribute("defaultColor") public String addDefaultColor() { return "Blue"; } @RequestMapping("/customize") public String customize() { return "customize"; } }
Binding Data with @ModelAttribute in Spring MVC:
Description: This example illustrates how to use @ModelAttribute
to bind data and add it to the model in a Spring MVC controller.
Code Snippet: (Controller)
// Controller class @Controller public class MyController { @ModelAttribute("user") public User getDefaultUser() { return new User("John Doe", 25); } @RequestMapping("/profile") public String userProfile() { return "profile"; } }
Spring MVC @ModelAttribute vs @RequestParam:
Description: This example compares the usage of @ModelAttribute
and @RequestParam
in a Spring MVC controller to handle form data.
Code Snippet: (Controller)
// Controller class @Controller public class MyController { // Using @RequestParam @RequestMapping("/processFormRequestParam") public String processFormRequestParam(@RequestParam("username") String username) { // Process username return "result"; } // Using @ModelAttribute @RequestMapping("/processFormModelAttribute") public String processFormModelAttribute(@ModelAttribute("user") User user) { // Process user object return "result"; } }
Handling Form Data with @ModelAttribute Annotation:
Description: This example demonstrates how to handle form data using the @ModelAttribute
annotation in a Spring MVC controller.
Code Snippet: (Controller)
// Controller class @Controller public class MyController { @RequestMapping("/showForm") public String showForm(Model model) { model.addAttribute("user", new User()); return "userForm"; } @RequestMapping("/processForm") public String processForm(@ModelAttribute("user") User user) { // Process user data return "result"; } }
Using @ModelAttribute for Model Binding in Spring MVC:
Description: This example showcases using @ModelAttribute
for model binding in a Spring MVC controller, where form data is automatically bound to a model object.
Code Snippet: (Controller)
// Controller class @Controller public class MyController { @RequestMapping("/editProfile") public String editProfile(@ModelAttribute("user") User user) { // User object is automatically populated with form data return "editProfile"; } }
Populating Form Data with @ModelAttribute in Spring:
Description: This example demonstrates how to use @ModelAttribute
to populate form data in a Spring MVC controller.
Code Snippet: (Controller)
// Controller class @Controller public class MyController { @RequestMapping("/showForm") public String showForm(Model model) { model.addAttribute("user", new User("John Doe", 25)); return "userForm"; } @RequestMapping("/processForm") public String processForm(@ModelAttribute("user") User user) { // Process user data return "result"; } }
ModelAttribute Annotation in Spring MVC Explained:
Description: This example provides an explanation of the @ModelAttribute
annotation in Spring MVC, its usage, and its role in handling form data.
Code Snippet: (Controller)
// Controller class @Controller public class MyController { @ModelAttribute("defaultColor") public String addDefaultColor() { return "Blue"; } @RequestMapping("/customize") public String customize() { return "customize"; } }