4

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

}
Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
blank
  • 17,852
  • 20
  • 105
  • 159

3 Answers3

2

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....)

Scott
  • 9,458
  • 7
  • 54
  • 81
1

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

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"/>
Community
  • 1
  • 1
gavenkoa
  • 45,285
  • 19
  • 251
  • 303
1

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)

Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
  • Thanks, a couple of things ... Spring 2.4 & I can't change the mapping value. I'm trying to avoid adding more methods to an already large controller. – blank Sep 28 '11 at 10:54
  • I don't understand the second option - my problem at the minute is that spring complains of two mapping values in separate controllers ... my underlying problem is a badly designed menu system which uses params and a form to map to different views. – blank Sep 28 '11 at 10:59
  • well, that's not that bad - you can have 1 method that takes a HttpServletRequest and check for each parameter – Bozho Sep 28 '11 at 11:06