【问题标题】:Thymeleaf not redirecting to index.htmlThymeleaf 没有重定向到 index.html
【发布时间】:2019-03-29 09:59:58
【问题描述】:

我已将 Thymeleaf 添加到我的 Spring Boot 项目中。

我在/resources/templates/index.html中创建了一个HTML文件

我在@RestController 中添加了一个方法:

@RequestMapping("/")
public String index(Model model, OAuth2Authentication authentication) {
    // irreveland code here
    return "index";
}

似乎这种方法有点用,但我没有将我重定向到http://localhost:8080/templates/index.html,而是有一个带有“索引”字样的白页(源代码中没有 html,只有字 index )

我已尝试将 index.html 页面放入 /resources/static 和 /resources 以进行测试 - 不费吹灰之力。

这里可能出了什么问题?

【问题讨论】:

    标签: java spring spring-boot thymeleaf


    【解决方案1】:

    您使用@RestController 注释了您的控制器,这意味着所有返回值都被视为响应主体(@ResponseBody)。这意味着您的字符串 "index" 被视为这样,而不是视图。

    为了使用 MVC 方法,其中 "index" 指的是名为 index.html 的视图,您应该使用 @Controller 注释。

    【讨论】:

    • 就是这样...我正在使用 RestController 测试我的 API 以与邮递员一起玩,但我忘了更改它。使用 Controller 注释,它就像一个魅力
    • 在上述情况下,您只会在浏览器中看到“index”一词,因为@RestContoller@Controller@ResponseBody 的组合。这只是返回作为文本直接写入 HTTP 响应的对象和对象数据。在您的情况下,您可能正在创建地图,然后提供用于显示结果的视图。这需要@Controller 注释。更多信息请阅读javarevisited.blogspot.com/2017/08/….>
    猜你喜欢
    • 2013-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-02
    • 2013-02-15
    • 1970-01-01
    • 2018-06-20
    相关资源
    最近更新 更多