Spring - download file and redirect

前端 未结 6 1803
渐次进展
渐次进展 2020-12-31 20:15

I have a download link on my page which works just fine but it doesn\'t refresh/redirects my page. Here\'s my code.

@RequestMapping(method = RequestMethod.PO         


        
6条回答
  •  醉酒成梦
    2020-12-31 20:44

    You can't download file and make refresh/redirect. I'll try to explain causes. Request flow is illustrated here: enter image description here

    where yellow circle is your controller. When you return view name front controller looks for appropriate view template (simply jsp, tiles or other, depending on configured view resolver) gets response and write generated html (or not html) code to it.

    In your case you perform actions:

    response.getOutputStream().write(baos.toByteArray());
    response.getOutputStream().close();
    response.getOutputStream().flush();
    

    After that actions spring can't open response and write refreshed page to it (because you do it before). So you can change your method signature to:

    public void exportToXML(HttpServletResponse response, Model model, @ModelAttribute(FILTER_FORM) ScreenModel form,
            BindingResult result, OutputStream out,
            HttpSession session) throws IOException {
    

    and delete last "return VIEW_NAME". Nothing will change.

提交回复
热议问题