This this tutorial you will learn about the Spring Annotation classes
This this tutorial you will learn about the Spring Annotation classesAnnotation is introduced since java 5. It is a new kind of instruction, syntax or meta data that provides data about the program and it is not a part of your program. Spring framework also provides a wide range of annotation, some of them which is commonly is used in Spring MVC is explained below.
@Controller - In Spring MVC you can make controller class very easily by prefixing @Controller before any class declaration.
@Controller public class MyController { }
@RequestMapping -The @RequestMapping annotation can be used with the classes or methods or both. It is used to specify the handler classes or methods that handles the particular request.
/* Used with Class */ @RequestMapping(value = "/page/") public class MyController { } /* Used with Method */ @RequestMapping(value = "/load-page") public String loadPage(Model model) { return "home-page"; }
@SessionAttribute - The Spring framework provides the more precise approach to keep the model object on the session by using SessionAttribute annotation.
@Controller @RequestMapping(value = "/load-page") @SessionAttributes("user") public class MyController { }
@ModelAttribute- The @ModelAttribute refers to the property of model class.
@RequestMapping(value = "/load-page") public String loadPage(@ModelAttribute("user") User user) { return "home-page"; }
@RequestParam- The @RequestParam is can be used to get the request parameters. It has three attributes value, required, and defaultValue. You can declare any number of @RequestParam annotations.
@RequestMapping(value = "/test-param") public String requestParam(@RequestParam(value = "name", required = true, defaultValue = "Default value") String name, @RequestParam(value="course")String course) { return "home-page"; }
@PathVariable- This annotation is used to access URI variable.
@RequestMapping(value = "/load-page/{userName}") public String pathVarible(@PathVariable(value = "userName") String userName) { return "home-page"; }