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

Spring Boot整合Thymeleaf+JPA+H2DB访问首页报TemplateInputException错误

报错根因

你遇到的报错是因为Thymeleaf渲染表单时,配置了th:object="${Book}"需要绑定对应名称的模型对象,但你访问/路径的控制器方法没有将空的Book对象放入Model中,导致模板解析时找不到对应属性抛出异常。此外你的页面代码还有一处拼写错误,也会导致后续提交报错。

修复步骤

  1. 修改BookController的index方法,添加模型属性
    将原index方法修改为如下代码,提前将空的Book对象放入Model,供Thymeleaf表单绑定:
@GetMapping("/")
public String index(Model model) {
    model.addAttribute("Book", new Book());
    return "index";
}
  1. 修正index.html的拼写错误
    页面中title对应的输入框th:field属性少写了首字母t,将:
<input type="text" th:field="*{itle}">

修改为:

<input type="text" th:field="*{title}">

可选优化建议

你自定义的Repository接口名称和Spring官方的org.springframework.data.repository.Repository重名,容易引发导入冲突,建议重命名为BookRepository更符合规范。
修改完以上内容后重启Spring Boot服务,即可正常访问首页。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.03 10:06:06