Summary of the six basic usage of @ RequestMapping in Spring mvc

Source: Internet
Author: User

Summary The usage of @ RequestMapping in spring mvc. 1) The most basic method-level application, for example, Java code @ RequestMapping (value = "/ments") public String simplePattern () {System. out. println ("simplePattern method was called"); return "someResult";} When you access http: // localhost/xxxx/parameters, the simplePattern method is called. 2) java code @ RequestMapping (value = "/ments") public String findDepatment (@ RequestParam ("departmentId") String parameter mentid) {System. out. println ("Find departm Ent with ID: "+ inclumentid); return" someResult ";} in the form of access:/orders? Using mentid = 23 triggers access to the findDepatment method. 3. The REST-style parameter Java code @ RequestMapping (value = "/attributes/{inclumentid}") public String findDepatment (@ PathVariable String inclumentid) {System. out. println ("Find department with ID:" + faster mentid); return "someResult";} is a REST-style address access, for example,/departments/23, here, we use (@ PathVariable to receive rest-style parameters 4 REST-style parameter binding form 2) to look at the example first. This is a bit like the previous: Java code @ RequestMapping (value = "/ments/ {Inclumentid} ") public String findDepatmentAlternative (@ PathVariable (" inclumentid ") String someDepartmentId) {System. out. println ("Find department with ID:" + somestmmentid); return "someResult";} This is a bit different, that is, receiving URL access in the form of/contents/23, 23 is used as the passed metnid, but in the actual method findDepatmentAlternative, use @ PathVariable ("departmentId") String some1_mentid to bind it to some1_mentid. So here some1_mentid is 23 5 The url is bound to multiple IDs at the same time. Java code @ RequestMapping (value = "/attributes/{inclumentid}/employees/{employeeId}") public String findEmployee (@ PathVariable String inclumentid, @ PathVariable String employeeId) {System. out. println ("Find employee with ID:" + employeeId + "from department:" + finding mentid); return "someResult" ;}this is actually quite understandable. 6 support regular expression Java code @ RequestMapping (value = "/{textualPart: [a-z-] + }. {numericPart: [\ d] +} ") public String regularExpression (@ PathVariable String textualPart, @ PathVariable String numericPart) {System. out. println ("Textual part:" + textualPart + ", numeric part:" + numericPart); return "someResult";} For example, output: Textual part: sometext, numeric part: 123.

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.