Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

spring-mvc

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?

like image 457
Steve Avatar asked Aug 14 '12 21:08

Steve


People also ask

How do I forward a Spring MVC request?

A request can be basically processed in three ways: a) resolved by Spring in a controller action, b) forwarded to a different controller action, c) redirected to client to fetch another URL. Forward: performed internally by Spring. the browser is completely unaware of forward, so its original URL remains intact.

What is the difference between forward and redirect in Spring MVC?

Forward: is faster, the client browser is not involved, the browser displays the original URL, the request is transfered do the forwarded URL. Redirect: is slower, the client browser is involved, the browser displays the redirected URL, it creates a new request to the redirected URL.

What is the use of redirect in Spring MVC?

Using the redirect prefix in your controller will generate a HTTP response with the 302 status code and the location header pointing to the redirection URL. The browser will then redirect to that URL (model exposed in the first request will be lost, and the browser URL will be the second one). Save this answer.


2 Answers

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

like image 190
danny.lesnik Avatar answered Oct 07 '22 01:10

danny.lesnik


You should be able to set the ssdh in a ModelAttribute and simply forward it back, this way, the RequestDispatcher should be able to map it back to the /results handler:

@RequestMapping(value = "/pubresult")
public String toPublicResultsScreen(ModelMap model,    
                                   HttpSession session,             
                                   @RequestParam (required=true) String LNAME,   
                                   @RequestParam (required=false)String FNAME, Model model){

  Search search = new Search(usertype);
  // Capture the search parameters sent by HTTP
  ssdh.setLast_name(LNAME);
  ssdh.setFirst_name(FNAME);
  model.addAttribute("ssdh", ssdh);

  return "forward:/results";         

}
like image 7
Biju Kunjummen Avatar answered Oct 07 '22 01:10

Biju Kunjummen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!