Summary of 6 basic usages of @requestmapping in Spring MVC

Source: Internet
Author: User

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

Java code

    1. @RequestMapping (value=< Span style= "color:blue;" > "/departments" )   

    2. public  string simplepattern () {  

    3. &NBSP;&NBSP;

    4.   system.out.println ( "Simplepattern  method was called ");   

    5. return   ;   

    6. }&NBSP;&NBSP;


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. }


   
  form such as access:  

  /departments?departmentid=23 can trigger access to the Finddepatment method  

3 Rest-style parameter  
  

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. }


  
 , such as the 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="/departments/{departmentid}")

  2. Public String finddepatmentalternative (

  3. @PathVariable ("DepartmentID") String Somedepartmentid) {

  4. System.out.println ("Find Department with ID:" + Somedepartmentid);

  5. return "Someresult";

  6. }



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 somedepartmentid here is

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="/{textualpart:[a-z-]+}.{ numericpart:[\\d]+} ")

  2. Public String RegularExpression (

  3. @PathVariable String Textualpart,

  4. @PathVariable String Numericpart) {

  5. System.out.println ("Textual part:" + Textualpart +

  6. ", Numeric part:" + Numericpart);

  7. return "Someresult";

  8. }



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


@requestmapping 6 Basic usage summaries 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.