@requestmapping basic usage of Spring MVC

Source: Internet
Author: User

Tag: expr das Print Value expression regular people size color

1. Application at the method level:

1 @RequestMapping (value= "/department")23 public String Simpledapatment () {45 System.out.println (""); 6 return ("result"); 7        

When accessing Http://localhost/XXXX/department, call the Simpledepatment method.

2. Parameter binding

@RequestMapping (value= "/department") public string Finddepartment (@RequestParaam (DepartmentID) string departmentid) {System.out.println ("Asasadas"); return ("Someresult");}

  

Visit http://localhost/XXXX/department?departmentId=22 to start the finddepartmentf method.

3, REST-style parameters

1 @RequestMapping (value= "/department/{departmentid}")23 Public String Find (@PathVariable string departmentid) {45 System.out.println ("ASA"); 6 7 return "Somerequest"; 8 9 }   

Access HTTP://LOCALHOST/XXXX/DEPARTMENT/22, @PathVariable receive rest-style parameters

4, REST-style parameters 2

1 @RequestMapping (value= "/department/{depatmentid}")  2  3 Public String Finddepartment (@PathVariable (DepartmentID) string somedepartmentid)  4  5{ 6 7 Syso; 8 9 Retrrn "SDS"; Ten  One}

Access to HTTP://LOCALHOST/XXXX/DEPARTMENT/22, the difference is that 22 was uploaded to DepartmentID, but in the actual method Finddepartment (@PathVariable (DepartmentID) String somedepartment),

Bind it to somedepartment, so Somedepartment is 22.

5. URL binding multiple IDs

1@RequestMapping (value= "/user/{userid}/admin/{adminid}")2 3  PublicString Findpeople (4 5 @PathVariable String userId,6 7 @PathVariable String Adminid) {8 9SYSO ("Userid+adminid");Ten  One return"SDFs"; A  -}

6. Support Regular Expressions

@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, the following url:/sometext.123, the output:
Textual Part:sometext, numeric part:123.

@requestmapping basic usage of Spring MVC

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.