sharad-garg
sharad-garg

Reputation: 469

How to pass the object from one controller to another in Spring without using Session

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.

Upvotes: 1

Views: 15009

Answers (3)

Yasir Shabbir Choudhary
Yasir Shabbir Choudhary

Reputation: 2578

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

Upvotes: 0

Debojit Saikia
Debojit Saikia

Reputation: 10632

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.

Upvotes: 4

Gerard Ribas
Gerard Ribas

Reputation: 727

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.

Upvotes: 2

Related Questions