SpringBoot集成thymeleaf做开发遇到的错误
Caused by: org.springframework.expression.spel.SpelEvaluationException: EL1007E: Property or field ‘name’ cannot be found on null
- 详细信息:(片段)
org.thymeleaf.exceptions.TemplateInputException: An error happened during template parsing (template: "class path resource [templates//admin/types-input.html]")
Caused by: org.attoparser.ParseException: Exception evaluating SpringEL expression: "name" (template: "/admin/types-input" - line 59, col 72)
at org.attoparser.MarkupParser.parseDocument(MarkupParser.java:393) ~[attoparser-2.0.5.RELEASE.jar:2.0.5.RELEASE]
at org.attoparser.MarkupParser.parse(MarkupParser.java:257) ~[attoparser-2.0.5.RELEASE.jar:2.0.5.RELEASE]
at org.thymeleaf.templateparser.markup.AbstractMarkupTemplateParser.parse(AbstractMarkupTemplateParser.java:230) ~[thymeleaf-3.0.11.RELEASE.jar:3.0.11.RELEASE]
... 53 common frames omitted
Caused by: org.thymeleaf.exceptions.TemplateProcessingException: Exception evaluating SpringEL expression: "name" (template: "/admin/types-input" - line 59, col 72)
at
org.springframework.expression.spel.SpelEvaluationException: EL1007E: Property or field 'name' cannot be found on null
at
-
原因:
静态页面使用了thymeleaf的表达式接收后台controller传输的对象(数据),但是后台却没有把这个对象传过来,或者传过来一个空对象,所有报错,意思是找不见这个字段(当然对象都没有哪来的字段)。 -
解决方案:
-
在yml配置文件添加:
mybatis: configuration: call-setters-on-nulls: true #设置返回字段不为空,前端不报错 -
后台controller层,再跳转页面时带一个空对象过去就行。例如:
/**
* 静态页面跳转
* @return
*/
@GetMapping("types/input")
public String typesInput(Model model) {
model.addAttribute("type", new Type());
return "/admin/types-input";
}
- 前端代码展示:
<!--表单提交-->
<form action="#" method="post" th:action="@{/admin/types}" th:object="${type}" class="ui form">
<!--分类名称-->
<div class="field">
<div class="ui left labeled input">
<label class="ui teal basic label">分类名称</label>
<input type="text" name="name" placeholder="请输入分类" th:value="*{name}">
</div>
</div>
本文深入分析了在SpringBoot项目中集成Thymeleaf模板引擎时遇到的常见错误——“找不到属性或字段name”,并提供了解决方案。错误源于控制器未正确传递对象或传递了空对象到前端,导致Thymeleaf无法找到相应的字段。通过在YAML配置文件中设置MyBatis的call-setters-on-nulls参数为true,并在控制器层预先填充一个空对象,可以有效避免此类错误。
6481

被折叠的 条评论
为什么被折叠?



