Spring MVC 3:在不同的控制器中具有相同的@RequestMapping,具有集中式XML URL映射(混合xml/注释方法) [英] Spring MVC 3: same @RequestMapping in different controllers, with centralised XML URL mapping (hybrid xml/annotations approach)
本文介绍了Spring MVC 3:在不同的控制器中具有相同的@RequestMapping,具有集中式XML URL映射(混合xml/注释方法)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我喜欢将所有映射保留在同一位置,因此我使用XML配置:
I like to keep all my mapping in the same place, so I use XML config:
<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>
如果我在另一个控制器中创建另一个具有相同名称的请求映射,
If I create a second request mapping with the same name in a different controller,
@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.
是否可以在不同的控制器中使用相同的请求映射?
Is there a way to use the same request mappings in different controllers?
我想要2个网址为
/video/info.html
/blog/info.html
使用Spring MVC 3.1.1
Using Spring MVC 3.1.1
I' not the only one: https://spring.io/blog/2008/03/24/using-a-hybrid-annotations-xml-approach-for-request-mapping-in-spring-mvc
该应用程序的其余部分运行正常.
The rest of the app works perfectly.
推荐答案
只需在Controller级别上放置一个请求映射:
Just put a requestmapping at the level of the Controller also:
@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 3:在不同的控制器中具有相同的@RequestMapping,具有集中式XML URL映射(混合xml/注释方法)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文