SpringMvc controller Annotation

Source: Internet
Author: User

SpringMvc controller Annotation

Before Spring2.5, we defined our processor class by implementing the Control interface or implementation.

Spring2.5 introduces annotation processor support, defines our class through @ controller and @ requestMapping annotations, and provides a powerful set of Annotations:

Requires processor ing

DefaultAnnotationHandlerMapping and the processor adapter AnnotationMethodHandlerAdapter are used to enable the processors supporting @ Controller and @ RequestMapping annotations.

@ Controller identifies a processor class

@ RequestMapping: The ing rule for requesting to the processor function method;

@ RequestParam: binds the request parameters to the method parameters of the processing method of the processor function;

@ ModelAttribute: Binding Request Parameters to command objects;

Spring3.0

The new HandlerMapping and HandlerAdapter support @ Controller and @ RequestMapping annotation processors.

New Annotation support classes: processor ing RequestMappingHandlerMapping

In combination with the processor adapter RequestMappingHandlerAdapter to replace the processor ultannotationhandlermapping starting with Spring2.5 and the processor adapter AnnotationMethodHandlerAdapter

Next, let's take a look at the basic annotations.

The following types of processor ing are allocated:

URL path ing: Use a URL to map a request to a processor; Restrictions on Request Method ing: for example, only GET requests are processed by the limited function; Request Parameter ing restriction: for example, only requests containing "abc" request parameters are allowed; Request Header ing restriction: for example, only requests with "Accept = application/json" are allowed.

Controller Implementation 1 Define processor @ Controller

@ Controller // ① declare a POJO class as a processor @ RequestMapping ("/ExamQueryQuestion") // ② map the request URL to the processing method of the processor function to public classQueryQuestionController {}

1.1 narrow request ing @ RequestMapping

Http: // 192.168.24.9: 8080/itoo-exam-questionbankmanage-web/ExamQueryQuestion/toQuestionTypesExcel

@ Controller // ① declare a POJO class as a processor

@ RequestMapping ("/ExamQueryQuestion") // common request prefix

Public class QueryQuestionController {

@ RequestMapping ("/toQuestionTypesExcel ")

Public StringtoQuestionTypesExcel (HttpServletRequest request,

HttpServletResponse resposne ){

Return "/ExamQueryQuestion ";

}

}

You can place @ Controller or @ RequestMapping on a POJO class to transform a POJO class into a processor;


2Spring profile spring-mvc.xml

  
  
  
  

3. View parser Configuration

Or use the previous org. springframework. web. servlet. view. InternalResourceViewResolver.

4. processor Configuration

  
  

You only need to register the processor implementation class to the spring configuration file. spring's DefaultAnnotationHandlerMapping or RequestMappingHandlerMapping can be automatically discovered Based on Annotation @ Controller or @ RequestMapping.

Summary:

This content is a small part of Spring. Its content is a simple injection method, and there are no configuration files. Of course, the configuration file injection method learned in the early stage also has its own advantages, there are few things to understand, and we can simply record two ways of learning.

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: info-contact@alibabacloud.com 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.