java Spring / Thymeleaf:在 null 上找不到属性或字段,但仍在呈现

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39630576/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 04:31:47  来源:igfitidea点击:

Spring / Thymeleaf: Property or field cannot be found on null, but still rendering

javasqlspringjdbcthymeleaf

提问by AppCrafter

I have a Spring / Thymeleaf app that

我有一个 Spring / Thymeleaf 应用程序

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:

有问题的html片段:

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

The field is part of the InstallLinkJoined object:

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

@Column(nullable = false)
private String projectName;

And I have getters and setters for all fields.

我有适用于所有领域的 getter 和 setter。

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?

我错过了什么?

回答by Prasanna Kumar H A

You are adding installattribute 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.

install通过检查 null添加属性,如果它为 null,则不会初始化任何内容,然后您将其放入 jsp 中th:text="${install.projectName}",所以它说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());
}