Summarize the usage of @RequestMapping in spring mvc.
1) The most basic, applied at the method level, for example:
When visiting http://localhost/xxxx/departments, the simplePattern method will be called
2) Parameter binding
An access form like this:
/departments?departmentId=23 can trigger access to the findDepatment method.
3 REST style parameters
REST-style address access, such as:
/departments/23, where (@PathVariable is used to receive rest-style parameters
4 REST-style parameter binding form 2) Let's
look at the example first, which is a bit like the previous one:
这个有点不同,就是接收形如/departments/23的URL访问,把23作为传入的departmetnId,,但是在实际的方法findDepatmentAlternative中,使用
@PathVariable("departmentId") String someDepartmentId,将其绑定为
someDepartmentId,所以这里someDepartmentId为23
5 url中同时绑定多个id
这个其实也比较好理解了。
6 支持正则表达式
For example, the following URL: /sometext.123 will output:
Textual part: sometext, numeric part: 123.
1) The most basic, applied at the method level, for example:
- @RequestMapping(value="/departments")
- public String simplePattern(){
- System.out.println("simplePattern method was called");
- return "someResult";
- }
When visiting http://localhost/xxxx/departments, the simplePattern method will be called
2) Parameter binding
- @RequestMapping(value="/departments")
- public String findDepatment(
- @RequestParam("departmentId") String departmentId){
- System.out.println("Find department with ID: " + departmentId);
- return "someResult";
- }
An access form like this:
/departments?departmentId=23 can trigger access to the findDepatment method.
3 REST style parameters
- @RequestMapping(value="/departments/{departmentId}")
- public String findDepatment ( athPathVariable String departmentId)
- System.out.println("Find department with ID: " + departmentId);
- return "someResult";
- }
REST-style address access, such as:
/departments/23, where (@PathVariable is used to receive rest-style parameters
4 REST-style parameter binding form 2) Let's
look at the example first, which is a bit like the previous one:
- @RequestMapping(value="/departments/{departmentId}")
- public String findDepatmentAlternative(
- @PathVariable("departmentId") String someDepartmentId){
- System.out.println("Find department with ID: " + someDepartmentId);
- return "someResult";
- }
这个有点不同,就是接收形如/departments/23的URL访问,把23作为传入的departmetnId,,但是在实际的方法findDepatmentAlternative中,使用
@PathVariable("departmentId") String someDepartmentId,将其绑定为
someDepartmentId,所以这里someDepartmentId为23
5 url中同时绑定多个id
- @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";
- }
这个其实也比较好理解了。
6 支持正则表达式
- @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 will output:
Textual part: sometext, numeric part: 123.