This @ModelAttribute Annotation can be applied on both methods as well as method-parameters. It accepts an optional “value”, which indicates the name of the attribute. If no value is supplied to the annotation, then the value would be defaulted to the return type name in case of methods and parameter type name in case of method-parameters.
Popular Tutorials
Let me explain you further with the helps of some examples.
1.
@Controller public class MyController { @ModelAttribute("myobject") public MyObject getInitializedMyObject() { return myService.getInitializedObject(); } @RequestMapping(value="/handle.htm", method=RequestMethod.GET) public ModelAndView handleRequest() { return new ModelAndView("myView"); } }
In this example, the value returned by getInitializedMyObject is added to the Model. The View would be able to retrieve this object using the key “myobject” from the request attributes.
2.
@Controller public class MyController { @ModelAttribute("myobject") public MyObject getInitializeMyObject() { return myService.getInitializedObject(); } @RequestMapping(value="/handle.htm", method=RequestMethod.GET) public ModelAndView handleRequest(@ModelAttribute("myobject") MyObject myObject) { myObject.setValue("test"); return new ModelAndView("myView"); } }
In this case, the getInitializeMyObject is executed first and the result is stored in a temporary map. This value is then passed as a parameter to the handler method. And finally myObject is added to the model for the views.
3.
@Controller @SessionAttributes("myobject") public class MyController { @RequestMapping(value="/handle.htm", method=RequestMethod.GET) public ModelAndView handleRequest(@ModelAttribute("myobject") MyObject myObject) { myObject.setValue("test"); return new ModelAndView("myView"); } }
In this case, Spring searches for “myobject” in the session and pass it as the parameter to the handler method. If “myobject” is not found in the session, then HttpSessionRequiredException is raised.
4.
@Controller public class MyController { @RequestMapping(value="/handle.htm", method=RequestMethod.GET) public ModelAndView handleRequest(@ModelAttribute("myobject") MyObject myObject) { myObject.setValue("test"); return new ModelAndView("myView"); } }
In this case, a new instance of MyObject is created and then passed to handler method. If MyObject is an interface or an abstract class, then a BeanInstantiationException is raised.
Spring MVC Related Posts
Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…
Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…
Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…
Technology has emerged a lot in the last decade, and now we have artificial intelligence;…
Managing a database is becoming increasingly complex now due to the vast amount of data…
Overview In this article, we will explore Spring Scheduler how we could use it by…