我可以在不同的Spring控制器中使用不同的参数使用相同的映射值吗?


问题内容

有什么办法可以完成这样的事情:我有一个用于导航的表单:

<form action="mapping.do">

   <input type="submit" value="menuOption01" />

   <input type="submit" value="menuOption02" />

</form>

PageController班是太大,有太多的依赖关系,我需要添加另一个菜单选项,但不希望添加了复杂性。我想在另一个处理新菜单选项的控制器中有一个方法。

尝试这样做会给我一个Spring配置错误(已经映射了处理程序):

@Controller
@SessionAttributes(types = { Entity.class })
class PageController {

    @RequestMapping(params = "menuOption01", value = "mapping.do")
    public String viewPage(@ModelAttribute final Entity entity) {
        ...
        return "view";
    }

    ... // another 5000 lines of code

}


@Controller
class OtherController {

    @RequestMapping(params = "menuOption02", value = "mapping.do")
    public String viewOtherPage(@ModelAttribute final Entity entity) {
        ...
        return "otherview";
    }

}

问题答案:

我遇到了类似的情况,因此我们为这些类型的方法制作了以下默认处理程序:

@RequestMapping(method = RequestMethod.POST, params = SIDE_TAB, value = "sideMenuController.xhtml")
public ModelAndView changeSelectedTab(@RequestParam(SIDE_TAB) String sideTab) {
  return new ModelAndView("redirect:/location/" + Utils.toCamelCase(sideTab) + ".xhtml");
}

然后,我们的页面具有以下内容:

<input type='submit' name='side-tab' value='$value' />

当然,这意味着我们必须为文件本身制定一个命名标准,但这很容易确保发生(即“事件历史记录”将转到eventHistory.xhtml,“创建新实体”将转到“
createNewEntity.xhtml” “,等等。)