开发者

Can I have the same mapping value with different param in a different Spring controller?

Is there any way to accomplish something like this: I have a form used for navigation :

<form action="mapping.do">

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

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

</form>

The PageController class is too big and has too many dependancies, I need to add another menu option but don't want to add to the complexity. I'd like to have a method in another controller which handles the new menu option.

Trying this gives me a Spring configutation error (There is already handler mapped):

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

}


I faced a similar situation so we made the following default handler for these types of methods:

@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");
}

Our pages then had the following:

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

This of course meant that we had to have a naming standard for the files themselves, but that was quite easy to ensure happened (i.e. "Event History" would go to eventHistory.xhtml, "Create New Entity" would go to "createNewEntity.xhtml", etc....)


Not directly, but:

  • You can include that param in the url: value=/mapping/parameter/ and /mapping/otherparameter. (The .do extension is a bit obsolete btw)

  • Use an if clause - pass the two params with @RequestParam("param", required=false) String param and use if (param != null) viewPage();

  • You can have one method that takes HttpServletRequest and checks whether a parameter with a given name exists (using request.getParameter("foo") != null)


You can use parameter per method mapping. See my question and answer:

  • @RequestMapping with "params" on same URL in different classes cause "IllegalStateException: Cannot map handler" in JUnit with SpringJUnit4ClassRunner
  • https://stackoverflow.com/a/14563228/173149

Just use these classes:

  <bean name="handlerMapping"
        class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>
  <bean name="handlerAdapter"
        class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜