SPRINGMVC Framework Note-taking (ii): NOTE-type controller-url path mapping __spring

Source: Internet
Author: User
Tags exception handling
Note-type controller

Define our processor class by @Controller and @RequestMapping annotations

spring2.5 need to map defaultannotationhandlermapping and processor adapters through the processor
Annotationmethodhandleradapter
To open the processor that supports @controller and @requestmapping annotations.

<!--Spring3.1 before the annotation handlermapping--> 
<bean class= " Org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping "/>
<!--Spring3.1 comments before Handleradapter--> 
<bean class= " Org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter "/>

Spring3.1 uses the new @contoller and @requestmapping annotation support classes:
Processor Mapping requestmappinghandlermapping
and Processor Adapter Requestmappinghandleradapter

<!--Spring3.1 started with annotations handlermapping-->
<bean
 class= " Org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping "/>

<!-- Spring3.1 started with annotations handleradapter-->
<bean
 class= " Org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter "/>

spring2.5+ defines the processor class through @Controller and @RequestMapping annotations through annotation processor support.

@Controller
Responsible for registering a bean into the spring context to identify the processor class;

@RequestMapping
NOTE Specifies which URL requests can be processed for the controller

@RequestParam
@RequestParam can pass the request parameter to the request method at the processing method entry parameter

@ModelAttribute
Spring MVC, before invoking the target processing method, the method, which is annotated on the method level at the @ModelAttribute, is called individually to get the object in the implied model data from the suppressed object, then to bind the request parameter to the object, and then into the arguments method entry object to add to the model @ Initbinder
Custom data-binding registration support for converting request parameters to the corresponding types of command object properties

restful architectural style annotation

@RequestBody
This annotation is used to read the body part data of request requests , using the system default configuration
The Httpmessageconverter is parsed,
The corresponding data is then bound to the object to be returned, and then the object data returned by Httpmessageconverter is bound to the parameter of the method in controller

@ResponseBody
The annotation is used to write the object returned by the Controller method to the body data area of the response object after the appropriate httpmessageconverter is converted to the specified format

@PathVariable
Binding URL placeholders to incoming parameters, request the template variable portion of the URI to the binding on the method parameter of the Processor function processing method

@ExceptionHandler
Annotation to method, this method is executed when an exception occurs

@ControllerAdvice
Make a Contoller a global exception handling class, in which @exceptionhandler method annotations are used to handle all controller occurrences

<!--autoenrollment defaultannotationhandlermapping based on annotation-style processors required, Annotationmethodhandleradapter-->
<MVC: Annotation-driven>

<!--registers a custom processor interceptor, SPRINGMVC does not have a total interceptor and cannot intercept all requests-->
<mvc:interceptors >

<mvc:interceptors>
<!--<!--will inject an interceptor--> <bean class= for each handlermapping. 
    Com.cpsh.inteceptor.mvc.MyInteceptor01 "/>
    <!--custom matching URL interceptor-->
    <mvc:interceptor>
        <mvc:mapping path= "/hello/*"/>
        <bean class= "Com.cpsh.inteceptor.mvc.MyInteceptor02"/>
    </ mvc:interceptor>
</mvc:interceptors>


<!--a logical static resource path to the;--> <mvc of a physical Static resource path
: resources>

<mvc:resources mapping= "/res/images/**" location= "/common/images/" cache-period= "31556926"/ >

Dispatcherservlet The default configuration is in Dispatcherservlet.properties (and Dispatcherservlet classes under one package) and is the default policy that is used when no configuration is specified in the spring configuration file

A Pojo class can be placed @Controller or @requestmappingto turn a Pojo class into a processor; @RequestMapping (value = "/hello") request URL (/hello) to The mapping of the processor's function processing method;

/**
    url = "${contextpath}/user/hello2"
* *
package cn.javass.chapter6.web.controller;
@Controller
@RequestMapping (value= "/user")//① generic mapping prefix for processors public
class HelloWorldController2 {
    @ Requestmapping (value = "/hello2")//② the mappings relative to ① to narrow public
    Modelandview HelloWorld () {
    //omitting implementation
    }
}

The request mapping can be considered to be the @requestmapping of the @requestmapping-level inheritance class at the method level. URL Path Mapping normal URL path mapping

@RequestMapping (value={"/test1", "/user/create"}): Multiple URL paths can be mapped to the same processor's function-handling method, combining the relationships that are or, that is, "/test1" or "/user/create" The request URL path can be mapped to the @requestmapping-specified feature processing method. URI Template Pattern mapping

@RequestMapping (value= "/users/{userid}"): {XXX} placeholder, the requested URL can be "/users/123456", through @pathvariable can be extracted in the URI template pattern in the {XXX} XXX variable.

@RequestMapping (value= "/users/{userid}/topics/{topicid}")

@RequestMapping (value= "/products/{categorycode:\d+}-{pagenumber:\d+}"): Can Match "/products/123-1, through @ xxx variable in {XXX: Regular expression-matched value} in pathvariable extraction mode

The regular expression-style URL path mapping is a special URI template pattern mapping:

The URI template pattern map is {userId} and cannot specify the data type of the template variable, such as a number or a string;

Regular expression style URL path mapping, you can specify the template variable data type, you can write the rules of a fairly complex request method mapping qualified

@RequestMapping (value= "/methodor", method = {requestmethod.post, requestmethod.get}): The request can be either get or POST.

Dispatcherservlet default on Get, POST, put, DELETE, head support;

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.