1) The most basic, method-level applications, such as:
Java code
-
@RequestMapping (value=< Span style= "color:blue;" > "/departments" )
-
public string simplepattern () {
-
&NBSP;&NBSP;
-
system.out.println ( "Simplepattern method was called ");
-
return ;
-
-
}&NBSP;&NBSP;
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 "Someresult";
-
-
}
form such as access:
/departments?departmentid=23 can trigger access to the Finddepatment method
3 Rest-style parameter
Java code
-
@RequestMapping (Value="/departments/{departmentid}")
-
Public String Finddepatment (@PathVariable string departmentid) {
-
-
System.out.println ("Find Department with ID:" + DepartmentID);
-
return "Someresult";
-
-
}
, 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
-
@RequestMapping (Value="/departments/{departmentid}")
-
Public String finddepatmentalternative (
-
@PathVariable ("DepartmentID") String Somedepartmentid) {
-
-
System.out.println ("Find Department with ID:" + Somedepartmentid);
-
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 "Someresult";
-
}
For example, the following url:/sometext.123, the output:
Textual Part:sometext, numeric part:123.
@requestmapping 6 Basic usage summaries in Spring MVC