小编典典

Spring / Thymeleaf:无法在null上找到属性或字段,但仍在渲染

sql

我有一个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;

我在所有领域都有吸气剂和吸气剂。

如果我将违规行注释掉,则仅在下一个变量处出现异常。

而且,如前所述,页面中的所有数据都显示出来了,因此显然模型对象不是null …

我想念什么?


阅读 175

收藏
2021-03-08

共1个答案

小编典典

install通过检查null添加属性,如果为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());
}
2021-03-08