SPRINGMVC Series (ii): Annotations @requestmapping, @PathVariable

Source: Internet
Author: User

First, @RequestMapping

[Email protected] In addition to modifying the method, can also modify the class
(1) Modifier class: Provides preliminary request mapping information, relative to the root directory of the Web request
(2) Modification method: Provide further subdivision mapping information
Relative to the URL at the class definition, if @requestmapping is not marked at the class definition, the URL marked at the method is relative to the Web app's root directory

How to request [email protected]
The value, method, params, and heads of the @RequestMapping represent request URLs, request methods, request parameters, and the mapping bar of the request header, respectively
, they are related to each other, and using multiple conditions together allows for more precise request mapping.

3.ANT style resource address supports 3 types of matching characters:
–?: Matches one character in a file name
–*: Matches any character in the file name
–**:** matching multi-layer paths
? @RequestMapping also supports ANT-style URLs:
–/user/*/createuser: Match
URLs such as/user/aaa/createuser,/user/bbb/createuser, etc.
–/user/**/createuser: Match
URLs such as/user/createuser,/user/aaa/bbb/createuser, etc.
–/user/createuser??: Match
URLs such as/user/createuseraa,/USER/CREATEUSERBB, etc.

1  Packagecom.study.springmvc.handlers;2 3 ImportOrg.springframework.stereotype.Controller;4 Importorg.springframework.web.bind.annotation.RequestMapping;5 ImportOrg.springframework.web.bind.annotation.RequestMethod;6 7@RequestMapping ("/requestmappingtest")8 @Controller9  Public classRequestmappingtest {Ten  One      Public Static FinalString success= "SUCCESS"; A     /** - * [email protected] In addition to the method can be modified, but also to modify the class - * (1) Modifier class: Provides preliminary request mapping information, relative to the root directory of the Web request the * (2) Modification method: Provide further subdivision mapping information - * Relative to the URL at the class definition, if @requestmapping is not marked at the class definition, the URL marked at the method is relative to the Web app's root directory -      * @returnSuccess -      */ +@RequestMapping ("/testrequestmapping") -      PublicString testrequestmapping () { +System.out.println ("Testrequestmapping"); A         returnSUCCESS; at     } -      -     /** - * [Email protected] Request Method - the value, method, params, and heads of the @RequestMapping represent request URLs, request methods, request parameters, and the mapping bar of the request header, respectively - , they are related to each other, and using multiple conditions together allows for more precise request mapping.  in      * @return -      */ to@RequestMapping (value= "/testmethod", method=requestmethod.post) +      PublicString TestMethod () { -System.out.println ("TestMethod post mode"); the         returnSUCCESS; *     }  $     Panax Notoginseng     /** - * 3. Understanding: You can use params and headers to map requests more precisely. Params and headers support simple expressions. the      *  +      * @return A      */ the@RequestMapping (value = "Testparamsandheaders", params = {"username"), +"Age!=10"}, headers = {"accept-language=zh-cn,zh;q=0.8" }) -      PublicString testparamsandheaders () { $System.out.println ("Testparamsandheaders"); $         returnSUCCESS; -     } -     /** the * 4.ANT style resource address supports 3 types of match characters: - *–?: Matches one character in a file nameWuyi *–*: Matches any character in the file name the *–**:** matching multi-layer paths -      * @return Wu      */ -@RequestMapping ("/testantpath/*/abc") About      PublicString Testantpath () { $System.out.println ("Testantpath"); -         returnSUCCESS; -     } -}
1 <%@ Page Language="Java"ContentType="text/html; Charset=utf-8"2 pageencoding="UTF-8"%>3     4 <!DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd ">5 <HTML>6 <Body>7 <!--href= "HelloWorld" and Helloworld.java's @requestmapping ("/helloworld") correspond -8 <ahref= "HelloWorld">Hello World</a>9 Ten <BR/> One <ahref= "Requestmappingtest/testrequestmapping">@RequestMapping modifying classes and methods</a> A  - <BR/> - <formAction= "Requestmappingtest/testmethod"Method= "POST"> the <inputtype= "Submit"value= "Test @requestmappin POST request Mode"> - </form> -   -  <BR/> + <ahref= "requestmappingtest/testparamsandheaders?username=lgs&age=11">Testparamsandheaders</a> -   +  <BR/> A <ahref= "REQUESTMAPPINGTEST/TESTANTPATH/GGGGGGG/ABC">Testantpath</a> at      -      - </Body> - </HTML>
Second, @PathVariable

Placeholder for @PathVariable map URL bindings

? The URL with placeholders is a new feature of Spring3.0, a milestone in the development of SPRINGMVC to REST goals

EM id= "__mcedel" style= "font-size:14px" > ? You can bind placeholder parameters in URLs to Process method entry: {XXX in URL The placeholder can be @PathVariable ("xxx") are bound to the entry parameters of the action method.

1 <  href= "PATHVARIABLETEST/TESTPATHVARIABLE/10">testpathvariable</  a>
1  Packagecom.study.springmvc.handlers;2 3 ImportOrg.springframework.stereotype.Controller;4 Importorg.springframework.web.bind.annotation.PathVariable;5 Importorg.springframework.web.bind.annotation.RequestMapping;6 7@RequestMapping ("/pathvariabletest")8 @Controller9  Public classPathvariabletest {Ten  One      Public Static FinalString success= "SUCCESS"; A      -     /** - * @PathVariable can map placeholders in URLs to parameters in the target method. the      * @paramID -      * @return -      */ -@RequestMapping ("/testpathvariable/{id}") +      PublicString testpathvariable (@PathVariable ("id") (Integer ID) { -System.out.println ("testpathvariable:" +ID); +         returnSUCCESS; A     } at}

Springmvc Series (ii): Annotations @requestmapping, @PathVariable

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.