【问题标题】:Spring / Thymeleaf: Property or field cannot be found on null, but still renderingSpring / Thymeleaf:在null上找不到属性或字段,但仍在渲染
【发布时间】:2016-09-22 04:57:10
【问题描述】:

我有一个 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 的 sn-p:

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

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

@Column(nullable = false)
private String projectName;

我对所有领域都有 getter 和 setter。

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

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

我错过了什么?

【问题讨论】:

    标签: java sql spring jdbc thymeleaf


    【解决方案1】:

    您通过检查 null 来添加 install 属性,如果它为 null 则不会初始化任何内容,然后您将在 jsp th:text="${install.projectName}" 中使用它,所以它是说 cannot be found on null。。 p>

    所以改成

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

    【讨论】:

    • 工作就像一个魅力!
    猜你喜欢
    • 1970-01-01
    • 2023-03-11
    • 2018-01-09
    • 1970-01-01
    • 2016-04-11
    • 2018-02-06
    • 1970-01-01
    • 2018-03-31
    • 2018-08-07
    相关资源
    最近更新 更多