Web MVC Controller Annotations
•        The Spring Web MVC controller has also been given a set of annotations to reduce its XML
configuration in Spring 2.5.
•        The MVC annotations are available for either the Web (a.k.a. Servlet) or Portlet MVC environments.
•        As discussed in the previous section, the @Controller annotation indicates that a particular bean serves
in the role of a controller component.
•        The @RequestMapping annotation is used in conjunction with the @Controller to map specific
requests to the controller.
•        Used on the class, the @RequestMapping annotation directs specific URLs to the controller.
@Controller
@RequestMapping("/browseOrders.request")
public class BrowseOrdersController extends AbstractController {

@Autowired
private OrderService orderService;

public void setOrderService(OrderService orderService) {  
this.orderService = orderService;
}

public ModelAndView handleRequestInternal(HttpServletRequest
request, HttpServletResponse response) throws Exception {
List list = orderService.findAllOrders();
return new ModelAndView("browseOrders", "orderList", list);
}
}

•        Used on the methods, the @RequestMapping annotation specifies the handler for particular method
(GET/POST/etc.) calls.
•        This allows any class to serve as controller and allow appropriate traffic to be guided to specific
methods.
@Controller
@RequestMapping("/browseOrders.request")
public class BrowseOrdersController {

@Autowired
private OrderService orderService;

public void setOrderService(OrderService orderService) {
 this.orderService = orderService;
}

@RequestMapping(method=RequestMethod.GET)
public ModelAndView handleRequest(HttpServletRequest
         request, HttpServletResponse response) throws
         Exception {
 List list = orderService.findAllOrders();
 return new ModelAndView("browseOrders", "orderList", list);
}
}

•        Now the controller class does not have to be listed as a bean in the bean configuration file.
•        However, don’t forget to signal the container to scan the appropriate package for controller (or any
other) stereotype components.
<context:component-scan base-package="com.intertech.web"/>
•        With these annotations, the controller also specifies its own URL mapping.
•        Therefore, URL mappings should not have to be configured in the bean configuration file with a handler
mapping.
•        Instead, register one of Spring 2.5’s annotation handler mapping beans as shown below.
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>

<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>
•        The URL/request mapping for multi-action controllers are typically done on methods since each
method responds to multiple URLs.
@RequestMapping("/add.request")
public void addHandler() {
...
}
@RequestMapping("/edit.request")
public void editHandler() {
...
}

•        Handler methods in the controllers are allowed to have varying signatures.
•         In other words, controller methods may have many arguments and various return types.
•        The table below specifies allowable argument types that can be used without further annotation in the
controller.
•        That is, the Spring container will automatically locate and pass these types of parameter objects to
@RequestMapping methods.
Parameter Type        Description
Request        Any request type like ServletRequest or HttpServletRequest
Response        Any response type like ServletResponse or HttpServletResponse
Session        HttpSession
WebRequest        org.springframework.web.context.request.WebRequest or NativeWebRequest
Locale        java.util.Locale
InputStream        java.io.InputStream
Reader        java.io.Reader
OutputStream        java.io.OutputStream
Writer        java.io.Writer
Map        java.util.Map
Model        org.springframework.ui.Model or org.springframework.ui.ModelMap
Command        Command/form objects
Errors        org.springframework.validation.Errors
BindingResult        org.springframework.validation.BindingResult
SessionStatus        org.springframework.web.bind.support.SessionStatus
•        Additionally, the @RequestParam annotation can be used to bind HTTP request parameters to a
method parameter.
public void addCustomer(@RequestParam("custId") int custId,
@RequestParam("custFirst) String firstName,
@RequestParam("custLast") String lastName) {
Customer c = new Customer(custId, firstName, lastName);
...
}
•        @RequestParam parameters are required by default.
•        To make them optional, set the required attribute to false.
@RequestParam(value="custId", required="false")

•        The @ModelAttribute annotation can also be used on method parameters to bind model data to method
parameters.
public String processSubmit(@ModelAttribute("cust") Customer customer) {
...
}
•        This use of @ModelAttribute gives the controller a reference to the model object typically holding form
data.
•        In fact, the @ModelAttribute annotation can also be used in front of a method.
•        When used in front of a method (method level), the @ModelAttribute annotation serves to mark a
method used to pre-populate the model.
•        @ModelAttribute annotated methods will get executed before the chosen @RequestMapping annotated
method in a controller.
•        In the example below, a date String is put into the model under the name of defaultOrderDate using the
method marked with @ModelAttribute.
@ModelAttribute("defaultOrderDate")
public String setDefault(){
return "Jan 01, 2008";
}

@RequestMapping(method=RequestMethod.GET)
public ModelAndView handleRequest(HttpServletRequest request,
HttpServletResponse response,
@ModelAttribute("defaultOrderDate") String defaultDate)
throws Exception {
System.out.println("The default order date is: " + defaultDate);
...
}
•        The pre-population of the data occurs before the @RequestMapping method is invoked
(handleRequest in this case).
•        The call of the pre-populating method is done automatically by the Spring container.
•        The second @ModelAttribute use in this example shows again how the model data can then be accessed
and bound to the method parameter.
Web MVC Controller Annotations
Table of Contents
Copyright (c) 2008.  Intertech, Inc. All Rights Reserved.  This information is to be used exclusively as an
online learning aid.  Any attempts to copy, reproduce, or use for training is strictly prohibited.