Spring MVC 3:不同控制器中的相同@RequestMapping,具有集中式XML URL映射(混合xml/注释方法)

2024-05-11

我喜欢将所有映射保留在同一个位置,因此我使用 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

EDIT:我不是唯一一个:https://spring.io/blog/2008/03/24/using-a-hybrid-annotations-xml-approach-for-request-mapping-in-spring-mvc https://spring.io/blog/2008/03/24/using-a-hybrid-annotations-xml-approach-for-request-mapping-in-spring-mvc

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


只需在控制器级别放置一个请求映射即可:

@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
    }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Spring MVC 3:不同控制器中的相同@RequestMapping,具有集中式XML URL映射(混合xml/注释方法) 的相关文章

随机推荐