Can I have the same mapping value with another parameter in another Spring controller?

Is there a way to do 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 PageController too large and has too many dependencies, I need to add another menu option, but I do not want to add to the complexity. I would like to have a method in another controller that processes the new menu item.

Trying this gives me a Spring configuration error (a handler has already been processed):

 @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"; } } 
+4
source share
3 answers

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

On our pages was the following:

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

This, of course, meant that for the files themselves we had to have a naming standard, but it was pretty easy to guarantee (for example, "Event History" will be sent to eventHistory.xhtml, "Create a new entity", createNewEntity.xhtml, etc.) d.)

+2
source

Not directly, but:

  • You can include this parameter in the URL: value=/mapping/parameter/ and /mapping/otherparameter . (The .do extension .do bit deprecated by btw)

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

  • You can have one method that accepts an HttpServletRequest and checks if the parameter with the given name exists (using request.getParameter("foo") != null )

+1
source

You can use parameter mapping for each method. See My Question and Answer:

  • @RequestMapping with "parameters" on the same URL in different classes raises "IllegalStateException: Can not map handler" in JUnit with SpringJUnit4ClassRunner
  • fooobar.com/questions/936210 / ...

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" />
+1
source

All Articles