⽬录
SpringMVC @RequestMapping不设置value从上个项⽬拷贝过来的form的action属性忘了修改了Spring mvc中@RequestMapping 6个基本⽤法⼩结1、最基本的,⽅法级别上应⽤2、参数绑定3、REST风格的参数4、REST风格的参数绑定形式之25、url中同时绑定多个id6、⽀持正则表达式SpringMVC @RequestMapping不设置value
从上个项⽬拷贝过来的form的action属性忘了修改了
在controller上没有设置@RequestMapping,⽅法上设置了但忘记设置value值,没想到还是能访问到,弄拙成巧,让我发现了这个问题
本以为会默认按照⽅法名来访问,当我修改⽅法名后依然能访问到猜想会以表单字段和参数列表的对应关系来访问,修改后依然能访问到
因为我这个项⽬所有的controller的命名空间没有⼀个是“account”,所以猜想当SpringMVC⽆法匹配到对应的controller时会访问@RequestMapping没有配置value的⽅法, 于是新建⼀个Controller类,也是只在⽅法上配置了⼀个没有设置value的@RequestMapping,
这时启动tomcat报错:
Caused by: java.lang.IllegalStateException: Ambiguous mapping found. Cannot map 'testController' bean method
public java.lang.String com.ld.controller.TestController.login(java.lang.String,java.lang.String,java.lang.String,javax.servlet.http.HttpServletRequest)to {[]}: There is already 'loginController' bean methodpublic java.lang.String
com.ld.controller.LoginController.login(java.lang.String,java.lang.String,java.lang.String,javax.servlet.http.HttpServletRequest) mapped.
at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.registerHandlerMethod(AbstractHandlerMethodMapping.java:212) at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.detectHandlerMethods(AbstractHandlerMethodMapping.java:184) at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.initHandlerMethods(AbstractHandlerMethodMapping.java:144) at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.afterPropertiesSet(AbstractHandlerMethodMapping.java:123) at
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping.afterPropertiesSet(RequestMappingHandlerMapping.java:126) at
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1633) at
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1570) ... 36 more
说明猜想正确,两个controller冲突了
后经测试,当@RequestMapping设置method属性,如⼀个⽅法上设置为GET,另⼀个设置为POST,http请求的⽅式不同,不会冲突,会找到对应的⽅法执⾏。
Spring mvc中@RequestMapping 6个基本⽤法⼩结
⼩结下spring mvc中的@RequestMapping的⽤法
1、最基本的,⽅法级别上应⽤
例如:
@RequestMapping(value=\"/departments\") public String simplePattern(){
System.out.println(\"simplePattern method was called\"); return \"someResult\"; }
2、参数绑定
@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风格的参数
@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
先看例⼦,这个有点象之前的:
@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 + \ return \"someResult\"; }
⽐如如下的URL:/sometext.123,则输出:
Textual part: sometext, numeric part: 123.
以上为个⼈经验,希望能给⼤家⼀个参考,也希望⼤家多多⽀持。
因篇幅问题不能全部显示,请点此查看更多更全内容