-->

弹簧MVC 3:相同@RequestMapping在不同的控制器,具有集中XML URL映射(混合X

2019-09-18 18:03发布

我喜欢让我所有的映射在同一个地方,所以我使用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

该应用程序的其余部分完美的作品。

Answer 1:

只要把一个requestmapping在控制器还级别:

@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
    }
}


Answer 2:

您可以使用每个方法映射参数。 见我的问题的答案:

  • 与在不同类别相同的URL“PARAMS” @RequestMapping原因:JUnit中与基于SpringJUnit4ClassRunner“IllegalStateException异常不能映射处理程序”
  • https://stackoverflow.com/a/14563228/173149


文章来源: Spring MVC 3: same @RequestMapping in different controllers, with centralised XML URL mapping (hybrid xml/annotations approach)