报错:EL1007E: Property or field 'name' cannot be found on null

本文深入分析了在SpringBoot项目中集成Thymeleaf模板引擎时遇到的常见错误——“找不到属性或字段name”,并提供了解决方案。错误源于控制器未正确传递对象或传递了空对象到前端,导致Thymeleaf无法找到相应的字段。通过在YAML配置文件中设置MyBatis的call-setters-on-nulls参数为true,并在控制器层预先填充一个空对象,可以有效避免此类错误。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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传输的对象(数据),但是后台却没有把这个对象传过来,或者传过来一个空对象,所有报错,意思是找不见这个字段(当然对象都没有哪来的字段)。

  • 解决方案:

  1. 在yml配置文件添加:

    mybatis:
      configuration:
        call-setters-on-nulls: true #设置返回字段不为空,前端不报错
    
  2. 后台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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值