From the course: Complete Guide to Spring MVC

Unlock this course with a free trial

Join today to access over 24,600 courses taught by industry experts.

Model

Model

- [Instructor] Model. What is a model attribute? A model attribute is like a container that holds data that you want to share between the controller and the view. So think the HTML pages rendered with Thymeleaf that we had earlier. It allows you to bind HTTP request data like form inputs to Java objects. And then it also allows you to pre-populate objects for use in your views. Again, it's like that item. Think of a suitcase or a container as I mentioned for carrying data around in a web application. For binding request parameters to Java objects, let's look at this example. Spring automatically maps request parameters, so like I mentioned, like form fields to the fields of a Java object. So in this example, if the request includes parameters like name equals John and email equals john@example.com, Spring will populate a user object with these values, binding the request parameters to the Java objects in your form fields. Model attribute can also be used at the method level to…

Contents