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

Thymeleaf变量表达式无法读取控制器Model对象数据报错问题

问题分析与解决方案

1. 控制器代码逻辑错误

你AdminProductController的index方法存在三处明显错误:

  • 方法名笔误:productRepository.finAll(pageable) 应为 productRepository.findAll(pageable),缺少字母d
  • 分页商品数据存入Model的键名不匹配:你存入时用的键是"product",但前端模板遍历用的是${products},修改如下:
// 原错误代码
model.addAttribute("product", product)
// 修改为
model.addAttribute("products", product)
  • 视图返回路径错误:Spring Boot默认Thymeleaf模板存放在resources/templates目录下,不需要写全路径,修改为和其他方法一致的返回格式即可:
// 原错误代码
return "/resources/admin/products/index.html"
// 修改为
return "admin/products/index"

2. 模板缺少Thymeleaf命名空间声明

你的index.html的<html>标签没有添加Thymeleaf命名空间,IDEA无法识别这是Thymeleaf模板,添加命名空间即可:

<html lang="en" xmlns:th="http://www.thymeleaf.org">

3. IntelliJ配置校验问题

如果修复上述代码后仍然报错,属于IDEA的静态校验配置问题,按以下步骤排查:

  • 检查项目是否启用了Thymeleaf支持:打开File > Project Structure > Facets,确认已经添加Thymeleaf facet,且模板前缀配置和你项目的实际模板目录匹配
  • 确认你的resources/templates目录已经被IDEA标记为模板资源根:右键目录 > Mark Directory as > Template Resources
  • 清除IDEA缓存重启:File > Invalidate Caches... 选择清除并重启即可

内容的提问来源于stack exchange,提问作者Mama

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.06 11:39:03