Thymeleaf:对象中的对象,无法像jsp那样访问值?属性或字段 - 无法在null上找到

时间:2016-06-30 10:06:38

标签: spring jsp spring-boot thymeleaf

我想访问以下值: 的 user.userSettings.nativeLanguage 地点:

User{
    private UserSettings userSettings
    //gettter & setter
}


UserSettings{
    private string nativeLanguage
    //gettter & setter
}

我的HTML:

       <table>
            <tr th:each="user:${friends}">
                <td><h3>Name:</h3><h3 th:text="${user.name}"></h3></td>
                <td><h3>Native:</h3><h3 th:text="${user.userSettings.nativeLanguage}"></h3></td>
                <td><h3>Learning: SOME</h3></td>
                <td><a href="/addAsFriend"  class="btn btn-primary btn-lg active">Add</a></td>
            </tr>
        </table>

它显示了这一行:

<h3>Native:</h3><h3 th:text="${user.userSettings}"></h3>

但这条线不起作用:

<h3>Native:</h3><h3 th:text="${user.userSettings.nativeLanguage}"></h3>

我得到了错误:

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

1 个答案:

答案 0 :(得分:0)

我的问题解决了: 我改变了:

@RequestMapping("/findfriend")
public String findFreind(Map<String, List<UserAccount>> users){
    users.put("friends", userRepository.findAll());         
    return "find_friend";
}

为:

@RequestMapping("/findfriend")
public String findFreind(Model model){
    model.addAttribute("friends", userRepository.findAll());        
    return "find_friend";
}