Spring Boot整合Thymeleaf+JPA+H2DB访问首页报TemplateInputException错误
报错根因
你遇到的报错是因为Thymeleaf渲染表单时,配置了th:object="${Book}"需要绑定对应名称的模型对象,但你访问/路径的控制器方法没有将空的Book对象放入Model中,导致模板解析时找不到对应属性抛出异常。此外你的页面代码还有一处拼写错误,也会导致后续提交报错。
修复步骤
- 修改BookController的index方法,添加模型属性
将原index方法修改为如下代码,提前将空的Book对象放入Model,供Thymeleaf表单绑定:
@GetMapping("/") public String index(Model model) { model.addAttribute("Book", new Book()); return "index"; }
- 修正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
相关产品推荐
相关产品推荐

