Reputation: 33
Raw beginner with Spring MVC -- that said, What component of Spring MVC passes in objects to a method annotated with @RequestMapping within a controller object?
For example,
@RequestMapping
public String test(Model model) {
model.addAttribute("testMessage", "My Test");
return "test";
}
Where does Model come from? Can my method take in any parameters I want? Is there some bit of intuitive Spring framework dependency injection that I'm simply not understanding here?
Upvotes: 0
Views: 184
Reputation: 26
This is all part of Spring dependency injection.
Spring injects the Model model
object to your controller method. Model model
is simply a Map <String, Object>
that stores attributes. When you added the attribute to the model
object using model.addAttribute("testMessage", "My Test")
its adding one entry to the Map
. This map is accessible from the view that you're interested in. So you can use this map in your view to access the attribute that you added from the controller (i.e. testMessage
)
There are several other things that you can pass in these controller methods that Spring resolves automatically and injects appropriate objects/value. You can use things like:
ModelMap modelMap
@RequestParam
@PathVariable
@ModelAttribute
BindingResult bindingResult
and on and on. When you pass these things on your controller method, Spring knows how to resolve them and inject them to your method. This might give you an better understanding of spring mvc: http://docs.spring.io/spring/docs/current/spring-framework-reference/html/mvc.html
Upvotes: 1