Spring4 MVC cheat sheet on Annotations

Spring 2.5 introduced support for annotation based MVC controllers. @RequestMapping, @RequestParam, @ModelAttribute are some of the annotations provided for this implementation.



@Controller Annotation

In spring-context first we need to declare a bean. This is just a stand spring bean definition in dispatcher’s context.
Latest way of doing this is, enabling autodetection. For the @Controller annotation spring gives a feature of autodetection. We can add “component-scan” in spring-context and provide the base-package.
 <context:component-scan base-package="com.javapapers.spring.mvc" />  
 <mvc:annotation-driven />   
 
Then add @Controller annotation to controllers. The dispatcher will start from the base-package and scan for beans that are annotated with @Controller annotation and look for @RequestMapping. @Controller annotation just tells the container that this bean is a designated controller class.

@RequestMapping Annotation

@RequestMapping annotation is used to map a particular HTTP request method (GET/POST) to a specific class/method in controller which will handle the respective request.
@RequestMapping annotation can be applied both at class and method level. In class level we can map the URL of the request and in method we can map the url as well as HTTP request method (GET/POST).
We can use wildcard characters like * for path pattern matching.
In the following example, @RequestMapping(“/hi”) annotated at class level maps the request url and at again at lower level method level mapping is used for HTTP request mapping.
package com.javapapers.spring.mvc;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
@RequestMapping("/hi")
public class HelloWorldController {

 @RequestMapping(method = RequestMethod.GET)
 public String hello() {
  return "hello";
 }

 @RequestMapping(method = RequestMethod.POST)
 public String hi(@RequestParam("name") String name, Model model) {
  String message = "Hi " + name + "!";
  model.addAttribute("message", message);
  return "hi";
 }

}

Multi-action Controller

In a multi-action controller urls are mapped at method level since the controller services multiple urls. In below example two urls are serviced by the controller and they are mapped to separate methods.
package com.javapapers.spring.mvc;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class HelloWorldController {

 @RequestMapping("/")
 public String hello() {
  return "hello";
 }

 @RequestMapping(value = "/hi", method = RequestMethod.GET)
 public String hi(@RequestParam("name") String name, Model model) {
  String message = "Hi " + name + "!";
  model.addAttribute("message", message);
  return "hi";
 }

}
When using multi-action form controller there is a possibility of creating ambiguity in mapping the urls to methods. In those cases, if we have specified a
org.springframework.web.servlet.mvc.multiaction.MethodNameResolver it will be used to map a method. If a method name resolver is not specified then by default org.springframework.web.servlet.mvc.multiaction.InternalPathMethodNameResolver is used. This default implementation doesnot support wildcard characters.
When a matching method is not found, action will be mapped to a default method in the controller which does not have any @RequestMapping specified. If there are more such methods in the controller, then method name will be taken into consideration.
<beans ...>
 
 <bean 
  class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping" />
 
 <bean class="com.javapapers.spring.mvc.controller.HelloWorldController">
   <property name="methodNameResolver">
    <bean class="org.springframework.web.servlet.mvc.multiaction.PropertiesMethodNameResolver">
      <property name="mappings">
 <props>
    <prop key="/">hello</prop>
    <prop key="/hi">hi</prop>
 </props>
       </property>
     </bean>
    </property>
  </bean>
 
</beans>

@RequestParam Annotation

To bind the request parameter a variable in method scope this @RequestParam annotation is used.
In below code “name” is bound to the request parameter.
 public String hi(@RequestParam("name") String name, Model model) {
  String message = "Hi " + name + "!";
  model.addAttribute("message", message);
  return "hi";
 }

@ModelAttribute Annotation

An annotated method parameter can be mapped to an attribute in a model using @Modelttribute in controller. It can also be used to provide reference data for the model when used at method level.
Mostly Used to handle form data and pass all fields to a pojo (details)


References:
1. http://javapapers.com/spring/spring-annotation-based-controllers/
2. http://viralpatel.net/blogs/spring-3-mvc-handling-forms/
3. List of steps to create a spring4-mvc web application http://crunchify.com/simplest-spring-mvc-hello-world-example-tutorial-spring-model-view-controller-tips/


Comments