This question nicely explains on how to write download file controllers in spring. This question nicely explains that Post request cannot be sent using response.sendRedirect()
I would like the user to be redirected to the same page with an error about what caused file download error. Here is the workflow
- User hits www.abc.com/index [controller has mapping /index.jsp and return ModelAndView]
- At this page, we have a file download which has URL www.abc.com/download?resource_id=123. [controller has mapping /download and returns void]
- When there is error in file download, user should be redirected to www.abc.com/index with some error display.
- When there is no error in file download, user stays at the same page and file download dialog appears.
Following is a snippet for forwarding:
@RequestMapping(/download)
public void execute(@RequestParam(value = "resource_id" required = true) final String resource, final HttpServletRequest request, final HttpServletResponse response) {
try {
//some processing
} catch {
RequestDispatcher dispatcher = request.getRequestDispatcher("/index" + "?is_downloaded=false");
dispatcher.forward(request, response)
}
}
@RequestMapping(/index)
public void execute(@RequestParam(value = "is_downloaded" required = false) final String isDownloaded, final HttpServletRequest request) {
//do stuff here
}
Step 3 is the problem.
- Using forwarding changes the URL to the download URL of the report on error.
- Using redirect as response.sendRedirect() with hidden parameters is impossible would not modify the URL at all.
- Using redirect as response.sendRedirect() with hidden parameters and would introduce "?is_downloaded=false" at the end of URL
Can anyone tell a workaround for this.