我喜欢将所有映射保存在同一个地方,因此我使用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
修改 我不是唯一一个: https://spring.io/blog/2008/03/24/using-a-hybrid-annotations-xml-approach-for-request-mapping-in-spring-mvc
应用程序的其余部分完美无缺。
答案 0 :(得分:5)
只需将一个请求映射放在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
}
}
答案 1 :(得分:2)