Question

Hi all question I'm having problems finding an answer for...

Use Case:

Reading in an Excel spreadsheet in a controller. A 4 row sheet was processed and row 2 and 3 had some errors in it, so I skip them and move on with the rest of the processing. But I want to retain these rows to display to the user after the processing is complete. I'd like to retain all the values in the row to display back to the user just to provide enough information.

So what I tried to do was this:

@RequestMapping(value = "/bulk-create", method = RequestMethod.POST)
public String bulkCreate(Model model, SpreadSheetFile spreadSheetFile){
  ...some code...

  List<Row> errorRows = new LinkedList<Row>();

  ...some code to process rows...

  //Error Found
  errorRows.add(row);

  ...more code to wrap up processing (possibly more errors)...

  model.addAttribute("erroRows", errorRows);
  return "redirect:/bulk-review"
}

@RequestMapping(value = "/bulk-review", method = RequestMethod.GET)
public String bulkReview(Model model, 
                         @ModelAttribute("errorRows")
                         LinkedList<Row> errorRows){
  model.addAttribute("errorRows", errorRows);
  return "bulk-review";
}

So basically I'm trying to pass a LinkedList from one MVC method to another without actually hitting a page first (unless I'm misunderstanding how "redirect:" works... it calls the controller method first correct?)

When I but a break point into the bulkReview method, errorRows is empty... So how do I retain this value between the two methods?

Thanks for the help! Let me know if I need to clarify anything! :)

Kris

Était-ce utile?

La solution

I don't think the redirect mechanism works the way you think it does. The first controller sends back a URL to the user's browser which the browser then follows. And by default, Spring places any data in the model at the time of a redirect in the redirect URL, so that the page-being-redirected to can "see" the model data. However, this doesn't work well when you have complex data in the model.

So, you need to think about how the data will be passed in the redirect URL that the browser sends back to the user. How does a List get serialized to the URL? probably not very well.

Another option would be to put the data in session from one controller and access it in the second.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top