The Model interface in Spring MVC is a holder for named attributes that a controller hands to the view. You add data with model.addAttribute("name", value), and the view technology (Thymeleaf, JSP, FreeMarker, Mustache) reads those attributes while rendering the HTML response.
Spring creates the Model for you and passes it into any handler method that declares it as a parameter. After the method returns a view name, the framework merges the model with that view and renders the page.
org.springframework.ui.Model is a small interface that exposes a map-like contract for request-scoped data. A controller fills it, and the DispatcherServlet passes it to the selected View together with the logical view name you returned.
The main methods are:
| Method | Purpose |
|---|---|
addAttribute(String name, Object value) | Adds one attribute under an explicit name |
addAttribute(Object value) | Adds an attribute with a name generated from the type (for example User becomes user) |
addAllAttributes(Map<String, ?> attributes) | Adds every entry of a map |
mergeAttributes(Map<String, ?> attributes) | Adds entries without overwriting existing keys |
containsAttribute(String name) | Checks whether a name is already present |
asMap() | Returns the attributes as a Map<String, Object> |
The default implementation is BindingAwareModelMap, which also keeps validation results next to the attributes they belong to.
The following Spring Boot controller loads a product and a list of related products, then exposes both to the template.
The template src/main/resources/templates/product/detail.html reads the attributes with Thymeleaf expressions:
The controller returns the string product/detail, which is a logical view name and not a file path. Thymeleaf resolves it to the template, and ${product} and ${related} come from the model. Thymeleaf escapes text by default with th:text, which protects you from cross-site scripting when you print user input.
Spring MVC accepts several types for the same job. They all end up as the same model data.
| Type | When to use it |
|---|---|
Model | The default choice. Declare it as a handler parameter |
ModelMap | A LinkedHashMap based class with the same addAttribute methods. Rarely needed in new code |
Map<String, Object> | Works, but gives you no helper methods and hides intent |
ModelAndView | Return value that carries both the view name and the model. Useful when you build the result programmatically |
Prefer Model plus a returned view name for most controllers. It is shorter, easier to test, and does not tie the method to a return type that carries presentation details.
The @ModelAttribute annotation works with the model in two ways.
On a method, it runs before every handler in the controller and contributes shared attributes, such as lookup lists for a form:
On a handler parameter, it binds request parameters to an object and puts that object in the model under the given name, so the view can redisplay submitted values after a validation error:
Place the BindingResult immediately after the object it validates, otherwise Spring throws an exception instead of letting you handle the errors.
Model attributes live only for the current request. If you return redirect:/orders, the browser issues a new request and the model is gone. Primitive and simple string values may be appended to the redirect URL as query parameters, which is rarely what you want.
Use RedirectAttributes to carry a one-time message across a redirect:
Flash attributes are stored in the session for one follow-up request and then removed. This is the standard post-redirect-get pattern.
addAttribute(Object) changes if you rename the class.@RestController and return objects. The Model is for server-rendered pages.No. Model attributes exist only for the current request. Use @SessionAttributes or HttpSession when data must survive several requests, and flash attributes for a single redirect.
You can declare it, but nothing renders it, so it has no effect on the JSON response. Return the object you want serialized instead.
Model is a parameter you fill while the method returns a view name. ModelAndView is a return value that holds both the model and the view name in one object. They produce the same result.
The usual causes are a misspelled attribute name, returning a redirect: view, or the method being a @ResponseBody handler. Check that the name in addAttribute matches the name in the template exactly.
The Model interface carries request-scoped data from a Spring MVC controller to the view. Declare it as a handler parameter, add attributes with explicit names, and return a logical view name. Combine it with @ModelAttribute for form binding, RedirectAttributes for redirects, and keep the data you expose small and view-oriented.