Spring MVC and Thymeleaf
In a typical Spring MVC application, @Controller
classes are responsible for preparing a model
map with data and selecting a view to be renered. This model map allows for the complete
abstraction of the view technology and, in the case of Thymeleaf, it is transformed in to a
Thymeleaf context object that makes all the defined variables available to expressions executed
in templates.
Spring MVC calls the piece of data that can be accessed during the execution of views model attributes commonly referred to in Thymeleaf language as context variables.
There are several ways of adding model attributes to a view in Spring MVC:
- Add attribute to
Model
via itsaddAttribute
method. - Return
ModelAndView
with model attributes included. - Expose common attributes via methods annotated with
@ModelAttribute
To access url parameters, there are also several methods.
- Use the
param.
prefix.
- Since parameters can be multivalued (e.g.
https://example.com/query?q=Thymeleaf%20Is%20Great! &q=Really%3F
) you may access them using brackets syntax
- by using the special
#request
object that gives you direct access to thejavax.servlet.http. HttpServletRequest
object