Spring-MVC 3.1: Forwarding A Request From One Controller Function To Another

Viewed 52302

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?

3 Answers

Use

org.springframework.web.servlet.view.RedirectView

class from spring package to redirect to different page in spring MVC controller. The Baeldung blog page has more details

Sample code:

@RequestMapping(value = "/", method = RequestMethod.GET)
public RedirectView mainMethod() {
    return new RedirectView("/login");
}

@RequestMapping(value = "/login", method = RequestMethod.GET)
public ModelAndView mainLogin() {
    ModelAndView model = new ModelAndView("login");
    return model;
}
Related