You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Boot集成Thymeleaf页面渲染异常问题求助

解决Spring Boot + Thymeleaf视图解析异常问题

问题根源

你在application.properties中配置的spring.mvc.view.suffix=.html是用于JSP视图解析器(InternalResourceViewResolver)的配置,会和Thymeleaf的视图解析逻辑冲突。Spring Boot在同时存在多个视图解析器时,会优先使用这个配置查找视图,导致无法正确识别Thymeleaf模板。

解决方案

  1. 移除冲突配置
    打开application.properties,删除以下行:

    spring.mvc.view.suffix=.html
    

    Thymeleaf自动配置已默认设置模板后缀为.html,无需额外配置该属性。

  2. 验证Thymeleaf基础配置(可选)
    你的spring.thymeleaf.prefix=classpath:/templates/是正确的,这也是Thymeleaf的默认值,可保留或直接删除该行(Spring Boot会自动使用默认配置)。

  3. 重启应用
    重新启动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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.19 22:50:33