gpt4 book ai didi

Spring MVC 3 : same @RequestMapping in different controllers, 带有集中式 XML URL 映射(混合 xml/注释方法)

转载 作者:行者123 更新时间:2023-12-04 14:55:24 25 4
gpt4 key购买 nike

我喜欢把所有的映射都放在同一个地方,所以我使用 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 中创建具有相同名称的第二个请求映射,
@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
}
}

我得到一个异常(exception):
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.

有没有办法在不同的 Controller 中使用相同的请求映射?

我想有 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

该应用程序的其余部分完美运行。

最佳答案

只需在 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 3 : same @RequestMapping in different controllers, 带有集中式 XML URL 映射(混合 xml/注释方法),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11255706/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com