Spring-MVC 3.1: Forwarding A Request From One Controller Function To Another
spring-mvc
Solution
You can use `RedirectAttributes` object which was introduced in Spring MVC 3.1 and populate it with data you want to keep for redirection. It called PRG (POST/Redirect/GET) pattern.
@RequestMapping(value="/saveUserDetails.action", method=RequestMethod.POST)
public String greetingsAction(@Validated User user,RedirectAttributes redirectAttributes){
//setting attributes
redirectAttributes.addFlashAttribute("firstName", user.getFirstName());
redirectAttributes.addFlashAttribute("lastName", user.getLastName())
return "redirect:success.html";
}
I wrote some technical article regarding how to use it. I believe it will give you more details:
http://www.tikalk.com/java/redirectattributes-new-feature-spring-mvc-31
Problem
I'm using Spring 3.1. I have a controller function that takes in a command object ( a data holder ) submitted via a FORM and does some processing : ``` @RequestMapping(value = "/results", method = RequestMethod.POST) public String toResultsScreen(@ModelAttribute("ssdh") SearchScreenDataHolder ssdh, BindingResult bindingResult, ModelMap model, HttpSession session) { if (bindingResult.hasErrors()) { logger.debug("Error returning to /search screen"); return "search"; } netView = "results"; // do stuff return nextView; } // end function ``` Some user would like to programmatically make GET links to obtain information from our site and I would like to set up another handler that would handle that request. It would create a new installation of that the command object ( ssdh ) and populate it with the parameters sent via the GET request. Then it would pass it on to the handler above. Something like this: ``` @RequestMapping(value = "/pubresult") public String toPublicResultsScreen(ModelMap model, HttpSession session, @RequestParam (required=true) String LNAME, @RequestParam (required=false)String FNAME){ Search search = new Search(usertype); // Capture the search parameters sent by HTTP ssdh.setLast_name(LNAME); ssdh.setFirst_name(FNAME); // To Do: "forward this data holder, ssdh to the controller function quoted first return nextView; } // end function ``` My question is how can I forward my command/data holder object to the first controller function such that I don't have to alter the code to the first controller function in any way?