我已将 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
,而是有一个白色页面,上面有一个单词“index” (来源中没有 html,只有单词 index)
我尝试将 index.html 页面放入/resources/static 和/resources 进行测试 - 没有任何努力。
这里可能会出现什么问题?
最佳答案
您使用@RestController
注释了您的 Controller ,这意味着所有返回值都被视为响应主体(@ResponseBody
)。这意味着您的字符串 "index"
会被视为字符串,而不是 View 。
为了使用 MVC 方法,其中 "index"
引用名为 index.html
的 View ,您应该使用 @Controller
注释。
关于java - Thymeleaf 没有重定向到index.html,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55414840/