Spring MVC 3:在不同的控制器中使用相同的@RequestMapping,使用集中的XML URL映射(混合xml /注释方法)
作者:互联网
我喜欢将所有映射保存在同一个地方,所以我使用XML配置:
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<value>
/video/**=videoControllerr
/blog/**=blogController
</value>
</property>
<property name="alwaysUseFullPath">
<value>true</value>
</property>
</bean>
如果我在不同的控制器中创建具有相同名称的第二个请求映射,
@Controller
public class BlogController {
@RequestMapping(value = "/info", method = RequestMethod.GET)
public String info(@RequestParam("t") String type) {
// Stuff
}
}
@Controller
public class VideoController {
@RequestMapping(value = "/info", method = RequestMethod.GET)
public String info() {
// Stuff
}
}
我得到一个例外:
Caused by: java.lang.IllegalStateException: Cannot map handler 'videoController' to URL path [/info]: There is already handler of type [class com.cyc.cycbiz.controller.BlogController] mapped.
有没有办法在不同的控制器中使用相同的请求映射?
我想要2个网址:
/video/info.html
/blog/info.html
使用Spring MVC 3.1.1
应用程序的其余部分完美运行.
解决方法:
只需将一个请求映射放在Controller的级别:
@Controller
@RequestMapping("/video")
public class VideoController {
@RequestMapping(value = "/info", method = RequestMethod.GET)
public String info() {
// Stuff
}
}
@Controller
@RequestMapping("/blog")
public class BlogController {
@RequestMapping(value = "/info", method = RequestMethod.GET)
public String info(@RequestParam("t") String type) {
// Stuff
}
}
标签:spring-mvc,spring,request-mapping 来源: https://codeday.me/bug/20190613/1232737.html