EL1008E:在'java.util.ArrayList'类型的对象上找不到属性或字段'content' - 可能不公开或无效?

时间:2018-06-11 13:41:43

标签: java spring-boot spring-security spring-data-jpa thymeleaf

我尝试为我的项目(电话簿)制作简单的过滤器,以便能够通过电子邮件而不是ID来查找用户联系人。当我只是浏览网址时:http://localhost:8080/home/phonebook。我收到以下错误。

错误消息

  

org.thymeleaf.exceptions.TemplateProcessingException:异常   评估SpringEL表达式:“data.content”(模板:   “/ home / phonebook” - 第29行,第13栏)

     

引起:   org.springframework.expression.spel.SpelEvaluationException:EL1008E:   在类型对象上找不到属性或字段“内容”   'java.util.ArrayList' - 可能不公开或无效?

的HomeController

@RequestMapping(value = {"/home/phonebook"}, method = RequestMethod.GET)
    public String showPage(Model model, @RequestParam(defaultValue = "0") int page){
        Authentication auth = SecurityContextHolder.getContext().getAuthentication();
        User user = userService.findUserByEmail(auth.getName());
        model.addAttribute("data",phonebookRepository.findAllByUserEmail(user.getEmail(),PageRequest.of(page,10)));
        model.addAttribute("currentPage",page);
        return "/home/phonebook";
    }

Phonebook.html

<tr th:each="phonebook :${data.content}">
        <td th:text="${phonebook.id}"></td>
        <td th:text="${phonebook.surname}"></td>
        <td th:text="${phonebook.firstname}"></td>
        <td th:text="${phonebook.phoneNumber}"></td>
        <td>
            <a th:href="@{delete/(id=${phonebook.id})}" class="btn btn-danger delBtn">Delete</a>
            <a th:href="@{findOne/(id=${phonebook.id})}" class="btn btn-primary eBtn">Edit</a></td>
    </tr>
    </tbody>
    </table>
        <hr/>
        <ul class="nav nav-pills">
            <li class="nav-item" th:each="i: ${#numbers.sequence(0,data.totalPages-1)}">
                <a th:href="@{/home/phonebook(page=${i})}" th:text="${i}" class="nav-link"
                   th:classappend="${currentPage}==${i}?'active':''"></a>
            </li>
        </ul>

PhonebookRepository

@Repository("phonebookRepository")
public interface PhonebookRepository extends JpaRepository<Phonebook,Integer> {
    List<Phonebook> findAllByUserEmail(String email, Pageable pageable);
}

SecurityConfig

@Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.jdbcAuthentication().dataSource(dataSource)
                .usersByUsernameQuery("select email, password, active from users where email=?")
                .authoritiesByUsernameQuery("select u.email, r.role from users u inner join user_role ur on(u.user_id=ur.user_id) inner join role r on(ur.role_id=r.role_id) where u.email=?")
                .passwordEncoder(passwordEncoder());

    }

更新1

<tr th:each="phonebook :${data}">进行了更改,我认为它修复了它,但我收到了一个新错误;

  

org.thymeleaf.exceptions.TemplateProcessingException:异常   评估SpringEL表达式:   “#numbers.sequence(0,data.totalPages-1)”(模板:“/ home / phonebook”    - 第42行,第38栏)

     

引起:   org.springframework.expression.spel.SpelEvaluationException:EL1008E:   在类型的对象上找不到属性或字段'totalPages'   'java.util.ArrayList' - 可能不公开或无效?

2 个答案:

答案 0 :(得分:3)

更改

<tr th:each="phonebook : ${data.content}">

<tr th:each="phonebook : ${data}">

因为您想迭代查询列表结果,但data.content只是一个属性。

答案 1 :(得分:0)

data.content只是一个属性,而是循环遍历$ data