Exposing Data to the View

Learn how to expose data to the view for rendering.

The method signature of controller methods is very flexible. Spring has dependency injection at the method and class level where we can use constructor injection (or field injection) to get references to dependent classes.

The addAttribute method

One of the types that can be injected into a controller method is org.springframework.ui.Model. This class allows us to add data (via the addAttribute(String, Object) method) that will be available to the view for rendering.

It will look something like this:

Get hands-on with 1400+ tech skills courses.