Summary of 6 basic usages of @requestmapping in Spring MVC

Source: Internet
Author: User

Summary of 6 basic usages of @requestmapping in Spring MVC (2013-02-18 11:57:46) reproduced
Category: Java notes
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= "/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 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= "/{textualpart:[a-z-]+}.{ numericpart:[\\d]+} ")
    2. Public String Regularexpression_r (
    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.

Summary of 6 basic usages of @requestmapping in Spring MVC

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.