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

标签 java sql spring jdbc thymeleaf

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

相关文章:

java - 创建一个桌面应用程序以将我们的输入状态显示为图形

mysql - 选择联接中*每*条记录包含字符串的位置

mysql - 获取字段总和

java - 在 Eclipse 上安装和使用 jersey

java - react excel 文件下载损坏

java - 无法解析相应的 jni 函数 opencv Android

java - 如何在 Tomcat 9.0.0M10 中修复 "JARs that were scanned but no TLDs were found in them "

Java 所见即所得的 HTML 编辑器

mysql - 执行插入语句时出现重复列名 'Unknown'错误,mysql

java - 测试 spring boot HandlerInterceptor 时避免 Controller 初始化