Summary of 6 basic usages of @requestmapping in Spring MVC

Source: Internet
Author: User

Summary of 6 basic usages of @requestmapping in Spring MVC

Summary of the usage of @requestmapping in spring MVC.

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

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


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

2) Parameter binding

Java code
    1. @RequestMapping (value="/departments")
    2. Public String Finddepatment (
    3. @RequestParam ("DepartmentID") String DepartmentID) {
    4. System.out.println ("Find Department with ID:" + DepartmentID);
    5. return "Someresult";
    6. }



Forms such as the form of access:

/DEPARTMENTS?DEPARTMENTID=23 can trigger access to the Finddepatment method.

3 Rest-style parameters

Java code
    1. @RequestMapping (value="/departments/{departmentid}")
    2. public string Finddepatment (@PathVariable string departmentid) {
    3. System.out.println ("Find Department with ID:" + DepartmentID);
    4. return "Someresult";
    5. }



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

4 Rest-style parameter binding form 2
First look at the example, this is a bit like before:

Java code
    1. @RequestMapping (Value=
    2. public string  Finddepatmentalternative (  
    3.    @PathVariable ( "DepartmentID")  string somedepartmentid) {  
    4.    
    5.     system.out.println (
    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, which is bound to
Somedepartmentid, so the somedepartmentid here is 23.

5 multiple IDs are bound at the same time in the URL

Java code
  1. @RequestMapping (value="/departments/{departmentid}/employees/{employeeid}")
  2. Public String Findemployee (
  3. @PathVariable String DepartmentID,
  4. @PathVariable String employeeId) {  
  5. System.out.println ("Find employee with ID:" + employeeId +
  6. "from department:" + DepartmentID);
  7. return "Someresult";
  8. }



This is actually better understood.

6 Support for regular expressions

Java code
    1. @RequestMapping (Value=
    2. public string  RegularExpression (  
    3.    @PathVariable  string  textualpart,  
    4.    @PathVariable  string  numericpart) {  
    5.   
    6.     system.out.println ( "textual part: "  + TEXTUALPART +   
    7.        ", numeric part: "  +  Numericpart);   
    8.     return 
    9. }  



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

Summary of 6 basic usages of @requestmapping 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.