时间:2023-01-09 11:46:28 | 栏目:JAVA代码 | 点击:次
在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 method
public 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的用法
例如:
@RequestMapping(value="/departments") public String simplePattern(){ System.out.println("simplePattern method was called"); return "someResult"; }
则访问http://localhost/xxxx/departments的时候,会调用 simplePattern方法了
@RequestMapping(value="/departments") public String findDepatment( @RequestParam("departmentId") String departmentId){ System.out.println("Find department with ID: " + departmentId); return "someResult"; }
形如这样的访问形式:
/departments?departmentId=23就可以触发访问findDepatment方法了
@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风格的参数
先看例子,这个有点象之前的:
@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
@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"; }
这个其实也比较好理解了。
@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"; }
比如如下的URL:/sometext.123,则输出:
Textual part: sometext, numeric part: 123.