Spring MVC 3.0 Controller

As noted before there are several articles about on how to program a Controller with Spring MVC. A lot of the examples however do not use Annotations. This article tries to unravel some of the mysteries.
A controller is basically a plain old java object (POJO) – however I am not so sure about the old part since Java 1.5 is requiered … Anyway, the class is annotated with @Controller(org.springframework.stereotype.Controller). A controller does the handling of the Requests of a Web page. If you do not want to do anything fancy, writing a controller does not necessitate the knowledge of how web applications work. You can have a Controller for each URL or a controller can handle different URLs. In the first case you would define the URL in the @RequestMapping(value = "/relative/path/to/your/page")(org.springframework.web.bind.annotation.RequestMapping) on the type. I prefer to have different URLs handled in the same controller. I put the RequestMapping without parameters on the type and use RequsetMapping on each handling method.
The most common parameters of the RequestMapping annotation are value where you store the relative URL defining the page that is handled and method that defines the org.springframework.web.bind.annotation.RequestMethod (such as GET, POST, PUT,DELETE,…). If you use RESTful URLs then the URL will contain variable parts, which you can access in your method. If you have a page product followed by the product ID the complete URL looks something like this http://www.example.com/product/4711. In this case you will define the value in your RequestMapping as follows:

@RequestMapping(value="/product/{productID}", method=RequestMethod.GET)

This annotates the method to retrieve the the page to display the product 4711. In the method signature you reference the product ID with the annotation @PathVariable(org.springframework.web.bind.annotation.PathVariable) as follows:

public String show(@PathVariable("productID") String productID,ModelMap modelMap){

The ModelMap is an empty Map in which you will put your data to be displayed on the page.

The POST request

If your method handles the POST of a form it gets a slight bit more complicated defining the method. First you will define the RequestMethod as POST. Then you have to add parameters to your signature to reference the form data. There are different ways:

  1. The post request might not be sent from a form but a button or some JavaScript function or even a small form. However the values sent are not that many. So you can define a variable for each value. Say your post data is the equivalent to sending a form with these two input fields:
    <input type="text" name="username"/>
    <input type="password" name="passwd"/>

    Then your signature your signature would look like this:

    public String login(@RequestParam("username") String username,
    			@RequestParam("passwd") String passwd,ModelMap modelMap)

    The values from the post request are filled into the appropriate arguments.
    If you use a model for your form as introduced in previous articles your signature looks like this:

    @RequestMapping(value = "/test", method = RequestMethod.POST)
    public String save(@ModelAttribute("customerForm") Customer customer, ModelMap modelMap)

    What else can be put in the signature can be found in the Spring documentation.


    Spring provides a mechanism to allow adding specific error messages to form fields. To pass the Object that contains this messages around simply add a org.springframework.validation.Errors argument to your signature:

    @RequestMapping(value = "/test", method = RequestMethod.POST)
    public String save(@ModelAttribute("customerForm") Customer customer, Errors errors, ModelMap modelMap)

    For further details see the Spring documentation.

    Harness the full power

    If you have ever implemented a Servlet you probably implemented a doGet or doPost method where you passed HttpServletRequest and HttpServletResponse. In a controller you generally do not need these object, but there may be some cases you need them e.g. if you need to know what in what language the page is displayed you need access to the header information of the request. Simply add an argument of type HttpServletRequest to the signature.

Ein Gedanke zu „Spring MVC 3.0 Controller“

Schreibe einen Kommentar