这是我的数据类调查这里没有什么不寻常的我甚至有急于加载来防止问题
package com.based.basedsurvey.data;
import jakarta.persistence.*;
import lombok.*;
import java.util.ArrayList;
import java.util.List;
@Entity
@Data
@NoArgsConstructor
public class Survey {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
@NonNull
private String name;
private boolean open;
@ToString.Exclude
@EqualsAndHashCode.Exclude
@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.EAGER)
private List<Question> questions = new ArrayList<>();
public Survey(String name){
this.name = name;
open = false;
}
}
控制器中的这也是非常标准的返回所有项
@GetMapping(path = "/")
public String homePage(Model model) {
model.addAttribute("surveys", surveyRepository.findAll(PageRequest.of(0,10)));
return "index";
}
库类
package com.based.basedsurvey.repo;
import com.based.basedsurvey.data.Survey;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.repository.CrudRepository;
public interface SurveyRepository extends JpaRepository<Survey, Long> {
Survey findSurveyById(long id);
}
html,这是一个小片段,重要的部分是surveyStat。odd和surveyStat.last
<tbody>
<tr th:each="survey: ${surveys}" th:class="${surveyStat.odd}? 'odd'">
<td>
<div th:attr="hx-reveal=${surveyStat.last ? 'revealed' : null}"></div>
<p th:text="${survey.isOpen()?'OPEN':'CLOSED'}"/>
</td>
</tr>
</tbody>
EL1021E: A problem occurred whilst attempting to access the property 'last': 'Unable to access property 'last' through getter method'
Unable to access property 'last' through getter method
Cannot invoke "java.lang.Integer.intValue()" because "this.size" is null
我不知道为什么当我使用surveyStat。奇怪的是,没有问题,但当我使用surveyStat。最后我给出了一个错误,索引页不再能够加载
编译时没有问题,但是当视图索引试图加载错误时,会抛出如下所示的错误EL1021E。
surveyRepository.findAll(PageRequest.of(0,10))
返回Page<T>
(如果您的surveyRepository
是默认的PagingAndSortingRepository)
这个Page
既不实现Collection
也不实现Map
,它不是一个数组。但是它实现了Iterable
,所以thymleaf将计算大小为null
。你可以在这里查看相关代码。
你可以做的是,而不是传递Page
给模型传递Collection
代替:
@GetMapping(path = "/")
public String homePage(Model model) {
List<Survey> surveys = surveyRepository.findAll(PageRequest.of(0,10)).getContent();
model.addAttribute("surveys", surveys);
return "index";
}