Annotations in Spring WEB MVC

Annotations in Spring WEB MVC

Spring Framework has provided annotations support right from its Spring 2.5 version. By using Annotations in Spring Web MVC applications we are able to reduce the following configurations in spring configuration file.

  • We can remove all Controller classes configuration.
  • We can remove HandlerMapping Class configuration.

Spring Web MVC Frame work has provided the following Annotations mainly.

  1. @Controller
  2. @RequestMapping
  3. @RequestParam
  4. @SessionAttributes
1. @Controller

Before Spring 2.5, all controller classes must be configured in spring configuration file, But, from Spring2.5 version, we are able to declare all controller classes with @Controller annotation to tell the Spring container that this class is a controller. In this context, to give an intimation to the Spring Container about the Controller classes and their locations we have to add the following tag in spring configuration file.

<context:component-scan base-package="package name where controller classes are existed" />

The above declaration (context:component-scan) tells the Spring container to auto scan the entire package to identify the controller components.

2. @RequestMapping

@RequestMapping annotation is used for defining the request URLs based on the context root and HTTP request methods like GET, POST, HEAD etc. for the request. This Annotation is used as either Class level annotation or Method level Annotation. 

Syntax

@RequestMapping(value="/---" , method=---)

Where value member will take Request URL with / prefix. Where method member will take the constants like GET, POST, HEAD etc. from RequestMethod enum.

E.g

@Controller
public class LoginController {
   @RequestMapping(value="/login", method=RequestMethod.POST)
   public ModelAndView checkLogin(HttpServletRequest request, HttpServletResponse response) {
      return new ModelAndView("success");
   }
}

Note

It is not mandatory to return ModelAndView object from controller method, where we can return String also but the parameter to Controller Method must be ModelMap, it will take model attributes which we want to submit to View JSP page.

E.g

@Controller
public class HelloController {
   @RequestMapping(value = "/hello", method = RequestMethod.GET)
   public String printHello(ModelMap model) {
      model.addAttribute("message", "Hello Spring MVC Framework!");
      return "hello";
   }
}
3. @RequestParam

@RequestParam annotation will be used in the methods to bind the method parameters to the request parameters. If the request is submitted by the browser, request parameters are passed through the URL query parameters. That can be easily mapped to the methods by annotating the method parameters with @RequestParam

Syntax

@RequestParam("paramName")

E.g

@Controller
public class LoginController {
@RequestMapping(value="/login", method=RequestMethod.POST)
   public ModelAndView checkLogin(@RequestParameter("uname") String uname, @RequestParameer("upwd") String upwd) {
      return new ModelAndView("success");
   }
}
4. @SessionAttributes

@SessionAttributes is used to define a variable name in order to keep its Key-Value pair in Session Scope and in order to use that in multiple pages in the same web application.

Syntax

@SessionAttributes("Param_Name") 

E.g

@Controller
@SessionAttributes("status")
public class LoginController {
   @RequestMapping(value="/login", method=RequestMethod.POST)
   public ModelAndView checkLogin(@RequestParameter("uname") String uname, @RequestParameer("upwd") String upwd, ModelMap map){
      map.addAttribute("status", "Login SUCCESS");
      return new ModelAndView("success");
   }
}
Annotations in Spring WEB MVC

Scroll to top