Summary of the usage of @requestmapping in spring MVC.
1) The 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 accessing 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
-
- }
Forms such as the form of access:
/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) {
-
- system.out.println ( "find department with id: " + departmentid);
- return "Someresult";
-
- }
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
- @RequestMapping (Value= "/departments/{ DepartmentID} ")
- public string Finddepatmentalternative (
- @PathVariable ( "DepartmentID") string somedepartmentid) {
-
- system.out.println (
- return "Someresult";
-
- }
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
- @RequestMapping (value="/departments/{departmentid}/employees/{employeeid}")
- Public String Findemployee (
- @PathVariable String DepartmentID,
- @PathVariable String employeeId) {
- System.out.println ("Find employee with ID:" + employeeId +
- "from department:" + DepartmentID);
- return "Someresult";
- }
This is actually better understood.
6 support for 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
- }
such as the following url:/sometext.123, the output:  
Textual Part:sometext, Numeric part:123.  
@requestmapping 6 Basic Usage Summary in Spring MVC (reprint)