@requestmapping basic usage in Spring MVC

Source: Internet
Author: User

1) The most basic, method-level applications, such as:

1 @RequestMapping (value= "/departments")  2 public String Simplepattern ( {  3   4   System.out.println ("Simplepattern method was called");   5   return "Someresult";   6   7 }  

When accessing Http://localhost/xxxx/departments, the Simplepattern method is called.

2) Parameter binding

1 @RequestMapping (value= "/departments")  2 public String finddepatment (   3   @RequestParam ("DepartmentID") String DepartmentID) {  4     5     SYSTEM.OUT.PRINTLN ("Find Department with ID:" + DepartmentID);   6     return "Someresult";   7   8 }  

The form of access:/departments?departmentid=23 can trigger access to the Finddepatment method.

3) Rest-style parameters

1 @RequestMapping (value= "/departments/{departmentid}")  2 Public String Finddepatment (@PathVariable string departmentid) {  3   4   SYSTEM.OUT.PRINTLN ("Find Department with ID:" + DepartmentID);   5   return "Someresult";   6   7 }  

  Like restful address access, such as:  /DEPARTMENTS/23, which is used (@PathVariable receive rest-style parameters  

4)   The 2  of the parameter binding form of rest style;
   look at the example first, this is a bit like before:  

 1  @RequestMapping (value= "/departments/{departmentid}" "  2  public   String finddepatmentalternative ( 3  @PathVariable (" DepartmentID ") String Somedepartmentid) { 4  5  System.out.println ("Find Department with ID:" + Somedepartmentid);  6  return  "Someresult" ;  7  8 } 

This is a bit different, is to receive a form such as/DEPARTMENTS/23 URL Access, the 23 as the incoming Departmetnid, but in the actual method finddepatmentalternative, using
@PathVariable ("DepartmentID") String Somedepartmentid, bind it to Somedepartmentid, so somedepartmentid here is 23

5) Multiple IDs are bound at the same time in the URL

1@RequestMapping (value= "/departments/{departmentid}/employees/{employeeid}")  2  PublicString Findemployee (3 @PathVariable String DepartmentID,4 @PathVariable String employeeId) {  5   6SYSTEM.OUT.PRINTLN ("Find Employee with ID:" + employeeId +7"From department:" +DepartmentID); 8     return"Someresult"; 9   Ten}

6) Support Regular expressions

 1  @RequestMapping (value= "/{textualpart:[a-z-]+}.{ numericpart:[\\d]+} ")  2  public   String regularexpression ( 3   @PathVariable String Textualpart,  4   @PathVariable String numericpart) { 5  6  System.out.println ("Textual part:" + Textualp  Art + 7  ", numeric part:" + Numericpart);  8  return  "Someresult" ;  9 } 

For example, the following url:/sometext.123, then output: Textual Part:sometext, numeric part:123.

@requestmapping basic usage in 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.