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

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";
    }

  • 前端代码展示:

        

你可能感兴趣的:(问题解决)