Current location - Quotes Website - Signature design - A controller has multiple methods, and there is no need to comment on how to match them.
A controller has multiple methods, and there is no need to comment on how to match them.
A simple annotation-based controller

Readers who use the lower version of Spring MVC know that when creating a controller, we need to implement the org directly or indirectly. Spring framework. web.servlet.mvc.controller interface. Generally speaking, we define our own controller by inheriting SimpleFormController or MultiActionController. After defining the controller, an important event is to define the mapping relationship between the request and the controller in the configuration file of Spring MVC through HandlerMapping, so as to associate the two.

Let's look at how the annotation-based controller defines how to do this. The following is the BbtForumController using annotations:

Listing 1. BbtForumController。 <。

Import com.baoaotao.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//& lt; ——①

@RequestMapping("/forum.do ")

Public class BbtForumController {

@ Auto Connect

Private bbtforumservice bbtforumservice;

@ request mapping//& lt; ——②

Public string listAllBoard() {

bbtforumservice . getall board();

System.out.println ("Call listAllBoard method." );

Return to the List Board;

}

}

From the above code, we can see that BbtForumController is no different from ordinary classes. It does not implement any special interface, so it is a real POJO. The wand that makes this POJO unique is the annotation of Spring MVC!

① Two annotations are used, namely @Controller and @RequestMapping. In the article "Using Annotation Driven IoC in Spring 2.5", the author pointed out that the annotations of @Controller, @Service, @Repository and @Component are equivalent: make a class a Bean of the Spring container. Since the controller of Spring MVC must be a Bean in advance, the annotation of @Controller is essential.

What really makes BbtForumController have the function of Spring MVC controller is annotation @RequestMapping. You can mark @RequestMapping in the class definition to associate the controller with a specific request; It can also be marked at the method signature to further transfer the request. In ①, we let BbtForumController associate the request of "/forum.do", while in ②, we specify the listAllBoard () method to handle the request. So @RequestMapping marked in the class declaration is equivalent to letting POJO implement the controller interface, while @RequestMapping in the method definition is equivalent to letting POJO extend the controller predefined by Spring (such as SimpleFormController, etc.). ).

In order to make annotation-based Spring MVC really work, we need to do something in the xxx-servlet.xml configuration file corresponding to Spring MVC. Before that, let's take a look at the configuration of web.xml:

Listing 2. Web.xml: Enable Spring Container and Spring MVC Framework

& lt? Xml version =" 1.0 "encoding ="UTF-8"? & gt

& ltweb-app xmlns="/xml/ns/javaee "

xmlns:xsi="/xml/ns/javaee

/XML/ns/javaee/we b-app _ 2 _ 5 . xsd " version = " 2.5 " >

& lt/display-name & gt;Spring annotation MVC example & lt/display-name >

& lt! Configuration file of Spring service layer->;

& lt context parameters & gt

& ltparam-name & gt; contextConfigLocation & lt/param-name & gt;

< parameter value > classpath: applicationcontext.xml <; /param-value & gt;

& lt/context-param & gt;

& lt! -Spring container launch [Crab New World]->

& lt Audience & gt

& lt listener class & gt org.springframework.web.context.contextloader listener

& lt/listener-class & gt;

& lt/listener & gt;

& lt! Servlet of Spring MVC will load web-INF/annomvc-servlet.xml.

Start the configuration file of Spring MVC module->;

& ltservlet & gt

& ltservlet-name & gt; annomvc & lt/servlet-name & gt;

& ltservlet-class & gt; org . spring framework . web . servlet . dispatcher servlet

& lt/servlet-class & gt;

& lt load & gt2 at startup & lt/load-on-startup >

& lt/servlet & gt;

& ltservlet mapping & gt

& ltservlet-name & gt; annomvc & lt/servlet-name & gt;

& lturl mode & gt *. do</URL-pattern >

& lt/servlet-mapping & gt;

& lt/we B- app & gt;

According to the contract of Spring MVC, a Spring MVC module named annomvc is defined in web.xml, and the specific configuration of Spring MVC module needs to be defined in the WEB-INF/annomvc-servlet.xml configuration file. The configuration of annomvc-servlet.xml is as follows:

Listing 3. annomvc-servlet.xml

& lt? Xml version =" 1.0 "encoding ="UTF-8"? & gt

& lt Douzi

xmlns = " ponent-scan base-package = " com . baobao Tao . web "/& gt;

& lt! -②: Start the annotation function of Spring MVC and complete the mapping between request and annotation POJO->;

& ltbean class = " org . spring framework . web . servlet . MVC . annotation。

AnnotationMethodHandlerAdapter "/& gt;

& lt! -③: Resolution of the model view name, that is, adding a suffix before the model view name->;

& ltbean class = " org . spring framework . web . servlet . view . internalresourceviewresolver "

p:prefix = "/we b-INF/JSP/" p:suffix = "。 JSP "/>;

& lt/beans & gt;

Because all the functions of Spring are evolved from beans, the controller must be turned into beans in advance, which is accomplished by marking @Controller in the class and enabling the component scanning mechanism in annomvc-servlet.xml, as shown in Figure ①.

In ②, an AnnotationMethodHandlerAdapter is configured, which is responsible for processing beans according to Spring MVC annotations in beans, making these beans become controllers and mapping specific URL requests.

③ The job in is to define the parsing rules of model view names. Here we use the special namespace of Spring 2.5, namely the P namespace, which needs to be passed.

Start Tomcat and send baobaotao.web

Import com.baoaotao.service.bbtforumservice;

Import org.springframework.beans.factory.annotation.autowired;

Import org.springframework.stereotype.controller;

Import org.springframework.web.bind.annotation.requestmapping;

@ controller

Public class BbtForumController {

@ Auto Connect

Private bbtforumservice bbtforumservice;

@ request mapping("/list all board . do ")//& lt; —— ①

Public string listAllBoard() {

bbtforumservice . getall board();

System.out.println ("Call listAllBoard method." );

Return to the List Board;

}

@ request mapping("/listboardtopic . do ")//& lt; —— ②

Common string listboardtopic (int topic) (

bbtforumservice . getboardtopics(topic id);

System.out.println ("Call listBoardTopic method." );

Return "listtopic";

}

}

Here, we mark @RequestMapping for listAllBoard () and listBoardTopic () methods at ① and ② respectively, and specify the URL requests handled by these two methods, which is equivalent to transforming BbtForumController into MultiActionController. In this way, the URL request of /listAllBoard.do will be processed by listAllBoard (), and /listBoardTopic.do? The URL request with topic = 1 is handled by the listBoardTopic () method.

For controllers that handle multiple URL requests, we prefer to specify the name of the controller's processing method through a URL parameter (for example, method=listAllBoard), rather than directly specifying the controller's processing method through different URLs. This common requirement can be easily realized by using the @RequestMapping annotation. Please look at the following code:

Listing 4. One controller corresponds to one URL, and the request processing method is determined by the request parameters.

Package com.baoaotao.web;

Import com.baoaotao.service.bbtforumservice;

Import org.springframework.beans.factory.annotation.autowired;

Import org.springframework.stereotype.controller;

Import org.springframework.web.bind.annotation.requestmapping;

@ controller

@ request mapping("/bbtforum . do ")//& lt; —— ① Specify the URL request corresponding to the controller.

Public class BbtForumController {

@ Auto Connect

Private bbtforumservice bbtforumservice;

//& lt; ② If the URL request contains the parameter "method=listAllBoard", this method will handle it.

@ request mapping(params = " method = list all board ")

Public string listAllBoard() {

bbtforumservice . getall board();

System.out.println ("Call listAllBoard method." );

Return to the List Board;

}

//& lt; ③ If the URL request contains the parameter "method=listBoardTopic", this method will handle it.

@ request mapping(params = " method = listBoardTopic ")

Common string listboardtopic (int topic) (

bbtforumservice . getboardtopics(topic id);

System.out.println ("Call listBoardTopic method." );

Return "listtopic";

}

}