blank
blank

Reputation: 18170

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

}

Upvotes: 4

Views: 3147

Answers (3)

gavenkoa
gavenkoa

Reputation: 48883

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"/>

Upvotes: 1

Bozho
Bozho

Reputation: 597254

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)

Upvotes: 1

Scott
Scott

Reputation: 9488

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

Upvotes: 2

Related Questions