Reputation: 771
I am using UrlBasedViewResolver. My requirement is that two different URls talk to same controller and they should go to two different pages aka page1 or page2 based on the URL. These two pages have same model object, they are almost same except few minor UI changes. How can this be achieved neatly in Spring MVC.
@RequestMapping(value = {"/page1","/page2"}, method=RequestMethod.GET)
public String displayPage(ModelMap map){
// return to Page1 or Page2 accordingly wherever it came from
}
@RequestMapping(value = {"/page1","/page2"}, method=RequestMethod.POST)
public ModelAndView submitPage(@ModelAttribute("model") Model model){
return new ModelAndView("page1 or page2", "command", model);
}
Upvotes: 1
Views: 2860
Reputation: 17867
One option:
@RequestMapping(value = {"/page1"}, method=RequestMethod.GET)
public String displayPage1(ModelMap map){
displayPageCommon(map);
return "Page1";
}
@RequestMapping(value = {"/page2"}, method=RequestMethod.GET)
public String displayPage2(ModelMap map){
displayPageCommon(map);
return "Page2";
}
private void displayPageCommon(ModelMap map){
//shared code
}
Another:
@RequestMapping(value = {"/{pageName}"}, method=RequestMethod.GET)
public String displayPage(@PathVariable String pageName, ModelMap map){
//shared code
return pageName;
}
Second option might have some issues depending on your controller mappings, and some potential security concern due to trying to return view based on incoming URL.
Upvotes: 2