Using the spring 2.5 annotation-driven spring MVC

Source: Internet
Author: User
Tags bind class definition


Following a major upgrade of Spring 2.0 to Spring MVC, Spring 2.5 introduced the annotation driver for spring MVC. Now you don't have to let Controller inherit any interfaces, do not need to define the mapping of the request and Controller in the XML configuration file, just using annotations can make a POJO have Controller most of the features--spring the ease of use of the MVC framework to a further enhancement. With framework flexibility, ease of use, and extensibility, spring MVC has gone beyond the rest of the MVC framework, and with spring singing Mengjin, the attraction of spring MVC in the MVC market will be increasingly irresistible.

This article describes the new sping MVC annotation feature in Spring 2.5, which describes how to replace the traditional XML-based Spring MVC configuration with the annotation configuration.

A simple Controller based on annotations

Readers who use the low version of Spring MVC know that when creating a Controller, we need to implement the Org.springframework.web.servlet.mvc.Controller interface directly or indirectly. In general, we define our Controller by inheriting Simpleformcontroller or Multiactioncontroller. After defining Controller, an important event is to define the mapping relationship between the request and the controller in the configuration file in Spring MVC, in order to correlate the two with the handlermapping.

Take a look at how the annotation based Controller is defined to do this, the following is the Bbtforumcontroller using annotations:

Listing 1.

package com.baobaotao.web;

import com.baobaotao.service.BbtForumService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.Collection;

@Controller          //<——① 
public class BbtForumController {

  private BbtForumService bbtForumService;

  @RequestMapping //<——② 
  public String listAllBoard() {
    System.out.println("call listAllBoard method.");
    return "listBoard";

From the code above, we can see that there is no difference between Bbtforumcontroller and general class, it does not implement any special interface, so it is an authentic POJO. The magic wand that makes this POJO unique is the annotation of Spring MVC!

Two annotations were used at the ①, @Controller and @RequestMapping respectively. In the article "using the Spring 2.5 annotation-driven IoC", I have pointed out that @Controller, @Service, and the role of @Repository and @Component annotations are equivalent: To make a class the be of the spring container An. Because the Controller of Spring MVC must be a Bean in advance, @Controller annotations are indispensable.

What really makes Bbtforumcontroller have the Spring MVC Controller feature is @RequestMapping this annotation. @RequestMapping can be annotated at the class definition to associate Controller with a specific request, and can be annotated at the method signature to further divert the request. At ①, we let Bbtforumcontroller associate "/" with the request, and ②, we specifically specify the Listallboard () method to handle the request. So the @RequestMapping that is labeled at the class declaration is equivalent to having POJO implement the Controller interface, and the @RequestMapping at the method definition is equivalent to POJO extending the Spring predefined Controller (such as Simp Leformcontroller, etc.).

In order for annotations-based spring MVC to work really, you need to do something with the Xxx-servlet.xml configuration file that corresponds to spring MVC. Before that, let's take a look at the configuration of Web.xml:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.