4

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

该应用程序的其余部分运行良好。

4

2 回答 2

5

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

@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
    }
}
于 2012-06-29T03:45:02.600 回答