Spring/Thymeleaf:无法在null上找到属性或字段,但仍在渲染 [英] Spring / Thymeleaf: Property or field cannot be found on null, but still rendering

查看:1651
本文介绍了Spring/Thymeleaf:无法在null上找到属性或字段,但仍在渲染的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Spring/Thymeleaf应用程序

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.

这是控制器:

@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的代码段:

A snippet of the html in question:

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

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

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.

而且,如上所述,页面中的所有数据都在显示,因此显然模型对象不是null ...

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

我想念什么?

推荐答案

您正在通过检查null添加install属性,如果为null,则不会初始化任何内容.那么您将其用于jsp th:text="${install.projectName}",则表示 cannot be found on null .

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.

因此更改为

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

这篇关于Spring/Thymeleaf:无法在null上找到属性或字段,但仍在渲染的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆