1

I have a requirement where the user selects some data from a form and we need to show that selected data on the next page.

At present we are doing this with a session attribute, but the problem with this is that it overwrites the data if the first page is open in another browser tab, where the data is again selected and submitted. So I just want to get rid of this session attribute while transferring data from one controller to another.

Note: I am using an XML based Spring configuration, so please show a solution using XML, not annotations.

Svante
  • 50,694
  • 11
  • 78
  • 122
sharad-garg
  • 359
  • 1
  • 6
  • 12

3 Answers3

4

Define RedirectAttributes method parameter in the the handler method that handles form submission from first page:

@RequestMapping("/sendDataToNextPage", method = RequestMethod.POST)
public String submitForm(
            @ModelAttribute("formBackingObj") @Valid FormBackingObj formBackingObj,
            BindingResult result, 
            RedirectAttributes redirectAttributes) {
    ...
    DataObject data = new DataObject();
    redirectAttributes.addFlashAttribute("dataForNextPage", data);
    ...
    return "redirect:/secondPageURL";
}

The flash attributes are saved temporarily before the redirect (typically in the session) and are available to the request after the redirect and removed immediately.

The above redirect will cause the client (browser) to send a request to /secondPageURL. So you need to have a handler method to handle this request, and there you can get access to the DataObject data set in the submitForm handler method:

@RequestMapping(value = "/secondPageURL", method = RequestMethod.GET)
public String gotoCountrySavePage(
            @ModelAttribute("dataForNextPage") DataObject data,
            ModelMap model) {
    ...
    //data is the DataObject that was set to redirectAttributes in submitForm method
    return "pageToBeShown";
}

Here DataObject data is the object that contains data from the submitForm method.

Roman Cherepanov
  • 1,639
  • 2
  • 24
  • 44
Debojit Saikia
  • 10,532
  • 3
  • 35
  • 46
  • OP note two things: `RedirectAttributes` use the `HttpSession` but with a specific key, so they won't overwrite your other attributes. `RedirectAttributes` are only available in Spring 3.1+. You can implement your own flash attributes with a `Filter` if you need to. There are examples online. – Sotirios Delimanolis Oct 25 '13 at 13:04
2

I worked with this requirement and I used RedirectAttributes, then you can add this redirect attributes to your model. This is an example:

@RequestMapping(value = "/mypath/{myProperty}", method = RequestMethod.POST)
public String submitMyForm(@PathVariable Long myProperty, RedirectAttributes redirectAttributes) {
    redirectAttributes.addFlashAttribute("message", "My property is: " + myProperty);
    return "redirect:/anotherPage";
}
@RequestMapping(method = RequestMethod.GET)
public String defaultPage(Model model, @RequestParam(required = false) String message) {
    if(StringUtils.isNotBlank(message)) {
        model.addAttribute("message", message);
    }
    return "myPage";
}

Hope it helps.

Gerard Ribas
  • 717
  • 1
  • 9
  • 17
  • I have a similar requirement and I can't seem to make it work. See this [link](http://stackoverflow.com/questions/44086540/how-to-send-object-that-is-retrived-in-a-get-request-to-a-post-requet-spring-b) It's pretty much what I want to do but there's no answer. Can you help? – Dashing Boy May 21 '17 at 19:54
0

You can use RedirectAttributes ; A specialization of the Model interface that controllers can use to select attributes for a redirect scenario.

public interface RedirectAttributes extends org.springframework.ui.Model

Plus this interface also provide a way to store "Flash Attribute" . Flash Attribute is in FlashMap.

FlashMap : A FlashMap provides a way for one request to store attributes intended for use in another. This is most commonly needed when redirecting from one URL to another. Quick Example is

@RequestMapping(value = "/accounts", method = RequestMethod.POST)
public String handle(RedirectAttributes redirectAttrs) {
 // Save account ...
 redirectAttrs.addFlashAttribute("message", "Hello World");
 return "redirect:/testUrl/{id}";
 }

Reference and detail information are here

Yasir Shabbir Choudhary
  • 2,458
  • 2
  • 27
  • 31