gpt4 book ai didi

java - Spring/Thymeleaf : Property or field cannot be found on null, 但仍在渲染

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:05:04 27 4
gpt4 key购买 nike

我有一个 Spring/Thymeleaf 应用程序

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

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

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

有问题的 html 片段:

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

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

@Column(nullable = false)
private String projectName;

而且我有所有字段的 getter 和 setter。

如果我注释掉有问题的行,我只会在下一个变量处得到一个异常。

而且,如前所述,页面中的所有数据都显示出来了,很明显模型对象不为空...

我错过了什么?

最佳答案

您正在通过检查 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());
}

关于java - Spring/Thymeleaf : Property or field cannot be found on null, 但仍在渲染,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39630576/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com