Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring mvc 无法在Spring MVC中重定向(使用“redirect/”)_Spring Mvc_Spring Boot_Redirect - Fatal编程技术网

Spring mvc 无法在Spring MVC中重定向(使用“redirect/”)

Spring mvc 无法在Spring MVC中重定向(使用“redirect/”),spring-mvc,spring-boot,redirect,Spring Mvc,Spring Boot,Redirect,我正试图发布一张图片。 我有一个表格要上传(连同包含所有上传图像的表格) 在我上传图片之前,一切正常。它重定向到URLlocalhost:8080/images显示字符串redirect://,而不是根目录。我做了另一个类似的应用程序(没有thymeleaf模板enfine),它运行良好。 我的控制器、服务或模板引擎中是否存在问题?您需要从方法中删除@ResponseBody注释-这告诉Spring响应将序列化为JSON(更多详细信息如下:) 如果删除该注释,它看起来应该可以工作。您需要从类中删

我正试图发布一张图片。 我有一个表格要上传(连同包含所有上传图像的表格)

在我上传图片之前,一切正常。它重定向到URL
localhost:8080/images
显示字符串
redirect://
,而不是根目录。我做了另一个类似的应用程序(没有thymeleaf模板enfine),它运行良好。
我的控制器、服务或模板引擎中是否存在问题?

您需要从方法中删除
@ResponseBody
注释-这告诉Spring响应将序列化为JSON(更多详细信息如下:)


如果删除该注释,它看起来应该可以工作。

您需要从类中删除@RestController,并且需要使用@Controller而不是@RestController。 您需要从类方法中删除@ResponseBody。因为@RestController和@ResponseBody将用于将响应序列化为JSON

<!DOCTYPE html>
<html lang="en" xmlns:th="https://thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <link rel="stylesheet" href="/main.css">
    <title>SpringImageApp</title>
</head>
<body>
    <div>
        <h3 th:if="${#vars['flash.message']}" th:text="${#vars['flash-message']}" class="flash"></h3>
        <h3 th:text="${page.number + 1} + ' of ' + ${page.totalPages}" />
            <table>
                <thead>
                <th>Id</th><th>Name</th><th>Image</th>
                </thead>
                <body>
                <tr th:each="image : ${page.content}">
                    <td th:text="${image.id}"/>
                    <td th:text="${image.name}"/>
                    <td th:text="@{'/images/' + ${image.name} + ' /raw '}"/>
                </tr>
                </body>
            </table>
            <form method="post" enctype="multipart/form-data" action="/images">
                <p><input type="file" name="file"></p>
                <p><input type="submit" value="Upload"></p>

            </form>
    </div>
</body>
</html> 
private static final String BASE_PATH="images"
@RequestMapping(method = RequestMethod.POST,value = BASE_PATH )
    @ResponseBody
    public String createFile(@RequestParam("file") MultipartFile file, RedirectAttributes redirectAttributes){

        try{
            imageService.createImage(file);
            redirectAttributes.addFlashAttribute("flash.message","Successfully uploaded" + file.getOriginalFilename());

        }catch(IOException e){

            redirectAttributes.addFlashAttribute("flash.message","Failure  uploaded" + file.getOriginalFilename());


        }
        return "redirect:/";

    }