java - Thymeleaf 没有重定向到index.html

标签 java spring spring-boot thymeleaf

我已将 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/

相关文章:

java - 我如何评估这个等式?

Java错误: <identifier> expected inside protected interface

java - 使用 SplitVerifier Java 7 到 Java 9

json - 如何在 Grails 中实现输入 JSON 验证?

LDAP 的 Spring Security Java 配置

java - ConcurrentMessageListenerContainer 在分区存在时减少并发计数

java - 使用 JBoss Cache 作为 Apache Lucene 的目录

Spring框架。如何存储一些数据以防止每次访问数据库

java - AuthorizationServerConfigurerAdapter 与 WebSecurityConfigurerAdapter 之间的区别

java - 如何获取 Spring Boot @Async 方法拒绝任务的详细信息?