Summary of 6 basic usages of @requestmapping in Spring MVC (2013-02-18 11:57:46)
reproduced
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 "Someresult";
- }
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 ("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 the somedepartmentid here is 23.
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_r (
- @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.
Summary of 6 basic usages of @requestmapping in Spring MVC