2016-09-22 18 views
2

我有一個Spring/Thymeleaf應用程序,春/ 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的問題一個片段:

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

領域是InstallLinkJoined對象的一部分:

@Column(nullable = false) 
    private String projectName; 

而且我有所有領域的獲得者和制定者。

如果我註釋掉違規行,我只是在下一個變量中得到異常。

而且,如前所述,在頁面中的所有數據被顯示出來如此明顯的模型對象是不是空...

我缺少什麼?

回答

3

你被檢查空加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()); 
} 
+0

工作就像一個魅力! – AppCrafter