Summary of 6 basic usages of @requestmapping in Spring MVC

Source: Internet
Author: User

1) Most basic, method-level applications, such as:    java code   @RequestMapping (value= "/departments")  public String Simplepattern () {     System.out.println ("Simplepattern method was called");    return " Someresult ";    }       When you visit Http://localhost/xxxx/departments, the Simplepattern method is called    2) Parameter binding    java code   @RequestMapping (value= "/departments")  public String finddepatment (   @RequestParam ("DepartmentID") String DepartmentID) {         System.out.println ("Find Department with ID:" + DepartmentID);      return "Someresult";    }        forms such as this Access form:     /departments?departmentid= 23 can trigger access to the Finddepatment method   3 Rest-style parameters    java code   @RequestMapping (value= "/ Departments/{departmentid} ")  public string finddepatment (@PathVariable string departmentid) { &NBsp;   System.out.println ("Find Department with ID:" + DepartmentID);    return "Someresult";    }       as a restful address, such as:  /DEPARTMENTS/23, which is used (@ Pathvariable receive rest-style parameters   4 Rest-style parameter binding form 2     See example First, this is somewhat like before:  java code  @ Requestmapping (value= "/departments/{departmentid}")  public String finddepatmentalternative (   @ Pathvariable ("DepartmentID") String Somedepartmentid) {       System.out.println ("Find Department with ID: "+ Somedepartmentid);      return "Someresult";   &NBSP,}        This is a bit different, is to receive a form like/DEPARTMENTS/23 URL access, the 23 as an incoming Departmetnid, But in the actual method finddepatmentalternative, use the   @PathVariable ("DepartmentID") String Somedepartmentid to bind it to   Somedepartmentid, so here Somedepartmentid the 23  5 URL to bind multiple Id  java codes   @RequestMapping (value = "/departments/{departmentid}/employees/{employeeid} ")  public string Findemployee (   @PathVariable string DepartmentID,    @PathVariable string E Mployeeid) {       System.out.println ("Find Employee with ID:" + employeeId +   &nbsp ;     "from department:" + DepartmentID);      return "Someresult";   &NBSP,        This is actually better understood.   6 supports regular Expressions    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"; &NBSP,}        For example url:/sometext.123, output:  textual part:sometext, Numeric part:123.
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.