Ii. requestmapping Annotations of Spring MVC

Source: Internet
Author: User

The last article was built up Hello world. Today, the main thing to see is the use and functionality of requestmapping annotations. The previous article said that requestmapping can be used to map URLs, which can be used on classes or on methods.


Requestmapping has the following properties: Value, method, params, headers, the following is a brief look at the function of the property

Value: The URL of the map request, when only a URL is defined, can not be written, that is, the following two ways equivalent:

@RequestMapping ("/testrequestmapping") @RequestMapping (value= "/testrequestmapping")

The Value property also supports wildcard mode:

? : Matches a single character

*: Match multiple characters, single-layer path matching

* *: Matching multi-layer paths

For example:

@RequestMapping ("/user/?/ef") can match:/user/a/ef,/user/s/ef,/user/y/ef,/user/3/ef@requestmapping ("/user/*/ef") Can match:/user/adf/ef,/user/gggs/ef,/user/y/ef,/user/fdfdf/ef@requestmapping ("/user/**") can match:/USER/ABC,/USER/ABD,/ User/abc/ef


Method: A total of four ways to define the request: Get, post, put, delete

Params: the definition of a request parameter that defines the values of parameters and parameters that must be passed for a request

Headers: Defines the requested header, params and headers two infrequently used


where params and headers support simple expressions

PARAM1: Indicates that the request must contain a request parameter named Param1!param1: Indicates that the request cannot contain a request parameter named param1 param1! = value1: Indicates that the request contains a parameter named param1, but its value cannot be value1{" Param1=value1 "," param2 "}: Indicates that the request must contain parameters named Param1 and Param2, and that the param1 parameter value must be value1

The following is a concrete example of how requestmapping annotations and their attribute definitions are used

1, @RequestMapping annotations defined on the class, my understanding, defined on the class, similar to the package in Java, the method accesses the path is: classpath/method Path, here is testrequestmapping/greeting

@Controller @requestmapping ("/testrequestmapping") public class Testrequestmappingcontroller {@RequestMapping ("/    Greeting ") Public String greeting () {return" greeting "; }}


2, the use of the method attribute, specify the way the request is post, if you use a GET request, it will appear 404

@RequestMapping (value = "/testmethod", method = requestmethod.post) public String TestMethod () {return ' greeting ';}

Test the code, the above link will appear 405, see:

<!--request failed because the request type was later defined as the method property of the post--><p> test annotation requestmapping, Access failed because the background defines the request as post <a href= "/ Testrequestmapping/testmethod ">test requestmapping method attr</a> </p><form action="/ Testrequestmapping/testmethod "method=" POST "> <p> request succeeded because using form form, defined as POST request </p> <input type=" Submit "value=" Submit "/></form>

650) this.width=650; "Src=" https://s4.51cto.com/wyfs02/M02/8E/76/wKioL1jBJuTCvfmSAABiz0QwEc0840.png-wh_500x0-wm_ 3-wmp_4-s_3318386199.png "title=" method validation. png "alt=" wkiol1jbjutcvfmsaabiz0qwec0840.png-wh_50 "/>

3. Use of the params attribute, params defines the requested parameter condition, supports expressions, supports arrays

/** * Use the params parameter to specify request parameters * Here's an example: the request must contain the request parameter name and age, and the value of age cannot be $ */testrequestmapping/testparams?name=jim&age=10: Request failed */TESTREQUESTMAPPING/TESTPARAMS?NAME=JIM&AGE=11: Request succeeded * @return */@RequestMapping (value = "/testparams", params = {"Name", "age!=10"}) public String Testparams () {return "greeting";}

Test the code, the above link will appear 404:

<p> Test note requestmapping The params property, which defines required name and age, and age cannot equal 10, access fails <a href= "/testrequestmapping/testparams? name=jim&age=10 ">test requestmapping </a></p><p> Test Note requestmapping the params property, The required name and age are defined later, and age cannot equal 10, access succeeds <a href= "/testrequestmapping/testparams?name=jim&age=11" >test Requestmapping </a> </p>


4, headers attribute, defines the request header related parameters, I use the Firefox browser, view the request header of the originating request, to set up the Accept-language to test:

650) this.width=650; "Src=" https://s4.51cto.com/wyfs02/M00/8E/77/wKiom1jBJULyFLHEAABjzb7APAs953.png-wh_500x0-wm_ 3-wmp_4-s_2003372704.png "title=" Firefox request header. png "alt=" wkiom1jbjulyflheaabjzb7apas953.png-wh_50 "/>


The following defines two methods, the above method and the browser consistent, can request success, the following method request failed


@RequestMapping (value = "/testhederssuccess", headers = {"accept-language=zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3"}) Public String testhederssuccess () {return "greeting";} @RequestMapping (value = "/testhedersfailure", headers = {"accept-language=zh-cn,zh;q=0.8"}) public String Testhedersfailure () {return "greeting";}


The test code is as follows, the above appears 404:

<p> Test note requestmapping Headers property, failed <a href= "/testrequestmapping/testhedersfailure" >test requestmapping </a> </p><p> Test note requestmapping headers properties, Success <a href= "/testrequestmapping/ Testhederssuccess ">test requestmapping </a></p>


5. The wildcard definition of the Value property, the following example, cannot match multiple levels,

For example cannot match:/testwildcardpath/abc/d/ef

@RequestMapping ("/testwildcardpath/*/ef") public String Testwildcardpath () {return "greeting";}

Test code: You can replace any of the request path with any string and cannot criticize multiple levels

<p>url wildcard form, you can convert any of the request path to any character, you can successfully <a href= "/testrequestmapping/testwildcardpath/any/ef" >test Requestmapping </a></p>


6, in conjunction with @pathvariable annotations, the URL of the Requestmapping value definition can contain placeholders, pathvariable annotations can map placeholders in the URL to the parameters of the method

@RequestMapping ("/testpathvariable/{id}") Public String testpathvariable (@PathVariable ("id") int id) {    SYSTEM.OUT.PRINTLN (ID); return "greeting";}

Test code: The following 11 is the parameter, the method can be received, can also pass different parameters

<p>pathvariable Test, 11 is the parameter, the method can be accepted to this parameter <a href= "/TESTREQUESTMAPPING/TESTPATHVARIABLE/11" >test Requestmapping </a></p>


Project Source code:

Https://git.oschina.net/acesdream/spring-mvc


This article is from "Ace's Dream" blog, please be sure to keep this source http://acesdream.blog.51cto.com/10029622/1904881

Ii. requestmapping Annotations of Spring MVC

Related Article

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.