Spring Boot集成Thymeleaf页面渲染异常问题求助
解决Spring Boot + Thymeleaf视图解析异常问题
问题根源
你在application.properties中配置的spring.mvc.view.suffix=.html是用于JSP视图解析器(InternalResourceViewResolver)的配置,会和Thymeleaf的视图解析逻辑冲突。Spring Boot在同时存在多个视图解析器时,会优先使用这个配置查找视图,导致无法正确识别Thymeleaf模板。
解决方案
移除冲突配置
打开application.properties,删除以下行:spring.mvc.view.suffix=.htmlThymeleaf自动配置已默认设置模板后缀为
.html,无需额外配置该属性。验证Thymeleaf基础配置(可选)
你的spring.thymeleaf.prefix=classpath:/templates/是正确的,这也是Thymeleaf的默认值,可保留或直接删除该行(Spring Boot会自动使用默认配置)。重启应用
重新启动Spring Boot应用后,访问http://localhost:8080/index,页面就能正常渲染出Hello, Augustine的内容。
额外说明
TaskController代码逻辑正确,返回的index会被Thymeleaf解析为classpath:/templates/index.html。- 页面中的
<th:block th:text="${name}">[name]</th:block>语法合法,Thymeleaf会正确替换模型中的name属性值。
内容的提问来源于stack exchange,提问作者Kirill
相关产品推荐
相关产品推荐

