如何在Spring MVC(3.0)中将两个URL路由映射到相同的处理程序方法?

时间:2010-10-12 22:58:12

标签: java spring spring-mvc url-routing code-duplication

我有userPanel方法映射到/user/panel网址:

@RequestMapping(value = "/user/panel", method = RequestMethod.GET)
public final String userPanel(HttpServletRequest request, ModelMap model)

但是,我还希望userPanel方法处理路由/panel而不创建单独的方法,如下所示:

@RequestMapping(value = "/panel", method = RequestMethod.GET)
public final String panel(HttpServletRequest request, ModelMap model)

有没有办法让userPanel方法处理这两条路由以避免重复?

1 个答案:

答案 0 :(得分:9)

@RequestMapping可以采用多种途径:

@RequestMapping(value = {"/user/panel", "/panel"}, method = RequestMethod.GET)
public final String userPanel(HttpServletRequest request, ModelMap model)