Spring mvc中@RequestMapping…
2014-03-20 17:27
99 查看
小结下spring mvc中的@RequestMapping的用法。
1)最基本的,方法级别上应用,例如:
Java代码
@RequestMapping(value="/departments")
public String simplePattern(){
System.out.println("simplePattern method was
called");
return "someResult";
}
则访问http://localhost/xxxx/departments的时候,会调用
simplePattern方法了
2) 参数绑定
Java代码
@RequestMapping(value="/departments")
public String findDepatment(
@RequestParam("departmentId") String
departmentId){
System.out.println("Find
department with ID: " + departmentId);
return "someResult";
}
形如这样的访问形式:
/departments?departmentId=23就可以触发访问findDepatment方法了
3 REST风格的参数
Java代码
@RequestMapping(value="/departments/{departmentId}")
public String findDepatment(@PathVariable String departmentId){
System.out.println("Find department with ID: " +
departmentId);
return "someResult";
}
形如REST风格的地址访问,比如:
/departments/23,其中用(@PathVariable接收rest风格的参数
4 REST风格的参数绑定形式之2
先看例子,这个有点象之前的:
Java代码
@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
Java代码
@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 支持正则表达式
Java代码
@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";
}
比如如下的URL:/sometext.123,则输出:
Textual part: sometext, numeric part: 123.
转:http://www.2cto.com/kf/201302/189407.html
1)最基本的,方法级别上应用,例如:
Java代码
@RequestMapping(value="/departments")
public String simplePattern(){
System.out.println("simplePattern method was
called");
return "someResult";
}
则访问http://localhost/xxxx/departments的时候,会调用
simplePattern方法了
2) 参数绑定
Java代码
@RequestMapping(value="/departments")
public String findDepatment(
@RequestParam("departmentId") String
departmentId){
System.out.println("Find
department with ID: " + departmentId);
return "someResult";
}
形如这样的访问形式:
/departments?departmentId=23就可以触发访问findDepatment方法了
3 REST风格的参数
Java代码
@RequestMapping(value="/departments/{departmentId}")
public String findDepatment(@PathVariable String departmentId){
System.out.println("Find department with ID: " +
departmentId);
return "someResult";
}
形如REST风格的地址访问,比如:
/departments/23,其中用(@PathVariable接收rest风格的参数
4 REST风格的参数绑定形式之2
先看例子,这个有点象之前的:
Java代码
@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
Java代码
@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 支持正则表达式
Java代码
@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";
}
比如如下的URL:/sometext.123,则输出:
Textual part: sometext, numeric part: 123.
转:http://www.2cto.com/kf/201302/189407.html
相关文章推荐
- springmvc controller @requestMapping 完成url请求通配
- Error creating bean with name 'org.springframework.web.servlet.mvc.method.annotation.RequestMappingH
- spring-mvc源码阅读(三)RequestMappingHandlerMapping
- Spring 的MVC @RequestMapping
- spring mvc No mapping found for HTTP request with URI
- springmvc No mapping found for HTTP request with URI in Dispatc
- springMVC No mapping found for HTTP request with URI
- @FeignClient with top level @RequestMapping annotation is also registered as Spring MVC handler
- SpringMvc @Requestmapping参数配置
- SSM-----springMVC No mapping found for HTTP request with URI的问题
- Spring MVC 3.0讲解
- 异常处理Spring MVC "The request sent by the client was syntactically incorrect ()"
- springMVC No mapping found for HTTP request with URI
- Support Classes for @RequestMapping methods in Spring MVC 3.1
- spring mvc中的handlermappin…
- No mapping found for HTTP request with URI [/spring/WEB-INF/page/index.jsp] in DispatcherServlet with name 'spring'
- springMVC No mapping found for HTTP request with URI
- SpringMVC 错误分析:@Autowi…
- Spring MVC @RequestMapping使用方法
- 关于Spring MVC的参数绑定