【问题标题】:why redirect not work in springmvc为什么重定向在spring mvc中不起作用
【发布时间】:2014-11-05 23:15:36
【问题描述】:

我想在上传后重定向,并想重定向“文件”页面。但是提交成功后,浏览器中的url没有重定向,我也使用firebug检查是否有任何重定向,但没有发生。 我将“redirect:/files”更改为“redirect:/files.jsp”也无济于事。 有什么错误吗?

这是我的代码:

@Controller
@RequestMapping({ "/files", "/files/" })
    public class FileAdminController {

     @RequestMapping(value = { "/upload/index", "/upload", "/upload/" }, method = RequestMethod.GET)
      public String showUplaod() {
        return "upload";
    }

     @RequestMapping(value = { "/index", "/index/", "/" }, method = RequestMethod.GET)
     public String showFilePage() {
        return "files";
    }

    @RequestMapping(value = "/upload", method = RequestMethod.POST)
    public @ResponseBody String handleFileUpload(
        @RequestParam("jarName") String jarName,
        @RequestParam("manifestName") String manifestName,
        @RequestParam("files") MultipartFile file) {

        try {

             File file1 = new File("c:/uploads/");

             file1.getParentFile().mkdirs();
             file1.createNewFile();
             BufferedOutputStream stream = new
             BufferedOutputStream(
             new FileOutputStream(file1));

             stream.write(bytes);
             stream.close();
            return "redirect:/files";
        } catch (Exception e) {
            return "You failed to upload " + jarName + " => " + e.getMessage();
        }


    }
}

【问题讨论】:

    标签: spring-mvc redirect response.redirect


    【解决方案1】:

    redirect: 是由UrlBasedViewResolver 解析的视图名称。但是使用@ResponseBody 你告诉spring 这个控制器没有返回视图名称。 IE。你必须自己处理重定向,注入HttpServletResponse

    【讨论】:

    • 您能否详细说明,因为我正面临这个问题,我的控制器中没有@ResponseBody
    猜你喜欢
    • 1970-01-01
    • 2016-01-01
    • 2016-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-18
    • 2021-10-31
    相关资源
    最近更新 更多