EL1008E: 在类型为“java.util.ArrayList”的对象上找不到属性或字段“内容” - 可能不是公共的或无效的?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/50799171/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
EL1008E: Property or field 'content' cannot be found on object of type 'java.util.ArrayList' - maybe not public or not valid?
提问by Ekera
I trying to make simple filter for my project(phonebook) to be able to find users contacts by their email instead of id. When I simply lauch the URL: http://localhost:8080/home/phonebook. I get the following error.
我试图为我的项目(电话簿)制作简单的过滤器,以便能够通过他们的电子邮件而不是 id 找到用户联系人。当我简单地启动 URL 时:http://localhost:8080/home/phonebook。我收到以下错误。
The error message
错误信息
org.thymeleaf.exceptions.TemplateProcessingException: Exception evaluating SpringEL expression: "data.content" (template: "/home/phonebook" - line 29, col 13)
Caused by: org.springframework.expression.spel.SpelEvaluationException: EL1008E: Property or field 'content' cannot be found on object of type 'java.util.ArrayList' - maybe not public or not valid?
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
电话簿.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());
}
UPDATE 1
更新 1
Made changes to <tr th:each="phonebook :${data}">
and i think it fixes it but i got a new error;
进行了更改<tr th:each="phonebook :${data}">
,我认为它修复了它,但出现了新错误;
org.thymeleaf.exceptions.TemplateProcessingException: Exception evaluating SpringEL expression: "#numbers.sequence(0,data.totalPages-1)" (template: "/home/phonebook" - line 42, col 38)
Caused by: org.springframework.expression.spel.SpelEvaluationException: EL1008E: Property or field 'totalPages' cannot be found on object of type 'java.util.ArrayList' - maybe not public or not valid?
org.thymeleaf.exceptions.TemplateProcessingException:评估 SpringEL 表达式的异常:“#numbers.sequence(0,data.totalPages-1)”(模板:“/home/phonebook”-第 42 行,第 38 列)
引起:org.springframework.expression.spel.SpelEvaluationException:EL1008E:在“java.util.ArrayList”类型的对象上找不到属性或字段“totalPages”——可能不是公共的或无效的?
采纳答案by lucumt
Change
改变
<tr th:each="phonebook : ${data.content}">
<tr th:each="phonebook : ${data.content}">
to
到
<tr th:each="phonebook : ${data}">
<tr th:each="phonebook : ${data}">
Because you want to iterate the query list result,but data.content
is just a property.
因为您要迭代查询列表结果,而data.content
只是一个属性。
回答by MRDJR97
data.content is just a property, loop through $data instead
data.content 只是一个属性,而是通过 $data 循环