Definition: A model attribute is a server-side key-value pair (String, Object).
Purpose: To share the results of business logic from the handler (Controller) to the View layer.
Who Creates: The request-handling Controller (handler), supplied by the programmer.
Who Sends to Whom: Handler sends it to the DispatcherServlet (D.S).
Process:
Accessing Model Attributes in JSP:
${requestScope.attrName}
org.springframework.web.servlet.ModelAndView
Definition: ModelAndView
is a class that holds model attributes and the logical view name.
Constructor:
ModelAndView(String forwardViewName, String modelAttrName, Object modelAttrVal)
Valid Return Types for Request Handling Method:
String
(logical view name)ModelAndView
Using: org.springframework.ui.Model
interface.
Definition: Represents a holder (Map) of model attributes.
How to Add Model Attributes:
public Model addAttribute(String modelAttrName, Object modelAttrVal)
Adding Multiple Model Attributes: Use method chaining.
Who Supplies the Empty Model Map: Spring Container (SC) via Dependency Injection (DI).