Spring / Thymeleaf:在null上找不到属性或字段,但仍然呈现(Spring / Thymeleaf: Property or field cannot be found on null, but still rendering)

我有一个Spring / Thymeleaf应用程序

org.springframework.expression.spel.SpelEvaluationException: EL1007E:(pos 0): Property or field 'projectName' cannot be found on null

但是,页面看起来很正常。 所有变量都使用数据进行渲染。 我只是担心每个请求都会抛出异常。

这是控制器:

@Controller @RequestMapping("/download") public class AppDownloaderController { @Autowired InstallLinkJoinedService installLinkJoinedService; @RequestMapping(value = "/link/{installLink}", method = RequestMethod.GET) public String getInstallLink(Model model, @PathVariable("installLink") String installLink) { InstallLinkJoined installLinkJoined = installLinkJoinedService.getInstallLinkWithID(installLink); if (installLinkJoined != null) { model.addAttribute("install", installLinkJoined); } return "download"; } }

有问题的html片段:

<h3 class="achievement-heading text-primary" th:text="${install.projectName}"></h3>

该字段是InstallLinkJoined对象的一部分:

@Column(nullable = false) private String projectName;

所有领域我都有吸气剂和制定者。

如果我注释掉有问题的行,我只是在下一个变量上得到一个例外。

并且,如上所述,页面中的所有数据都显示出来,因此模型对象显然不为空...

我错过了什么?

I have a Spring / Thymeleaf app that

org.springframework.expression.spel.SpelEvaluationException: EL1007E:(pos 0): Property or field 'projectName' cannot be found on null

However, the page looks normal. All variables are rendering with data. I'm just concerned that an exception is being thrown every request.

Here is the controller:

@Controller @RequestMapping("/download") public class AppDownloaderController { @Autowired InstallLinkJoinedService installLinkJoinedService; @RequestMapping(value = "/link/{installLink}", method = RequestMethod.GET) public String getInstallLink(Model model, @PathVariable("installLink") String installLink) { InstallLinkJoined installLinkJoined = installLinkJoinedService.getInstallLinkWithID(installLink); if (installLinkJoined != null) { model.addAttribute("install", installLinkJoined); } return "download"; } }

A snippet of the html in question:

<h3 class="achievement-heading text-primary" th:text="${install.projectName}"></h3>

The field is part of the InstallLinkJoined object:

@Column(nullable = false) private String projectName;

And I have getters and setters for all fields.

If I comment out the offending line, I simply get an exception at the next variable.

And, as mentioned, all the data in the page is showing up so obviously the model object is not null...

What am I missing?

最满意答案

你通过检查null来添加install属性,如果它是null,那么什么都不会被初始化然后你在jsp th:text="${install.projectName}"它,所以它说cannot be found on null

所以改为

InstallLinkJoined installLinkJoined = installLinkJoinedService.getInstallLinkWithID(installLink); if (installLinkJoined != null) { model.addAttribute("install", installLinkJoined); } else { model.addAttribute("install", new InstallLinkJoined()); }

You are adding install attribute by checking null,if it's null then nothing will be initialized & then you are taking that in jsp th:text="${install.projectName}",so it's saying cannot be found on null.

So change to

InstallLinkJoined installLinkJoined = installLinkJoinedService.getInstallLinkWithID(installLink); if (installLinkJoined != null) { model.addAttribute("install", installLinkJoined); } else { model.addAttribute("install", new InstallLinkJoined()); }

更多推荐

null,String,download,projectName,电脑培训,计算机培训,IT培训"/> <meta name=&q