可以在frienddet
内检索会话getStatusList(
,还是应该在构造函数中检索它并将其分配给变量。当我在调试模式下运行eclipse时,我发现在遇到以下代码行时多次调用getStatusList()
函数:
<a4j:repeat var="p" value="#{friendBean2.statusList}"
iterationStatusVar="loop">
在第五次它给我错误:
Error reading 'statusList' on type com.bean.FriendBean2
当我找到问题的根源时,会话中缺少frienddet
。这就是让我疯狂的原因
1)FriendBean2
public List<Status> getStatusList() {
EntityManager em;
EntityManagerFactory emf = Persistence
.createEntityManagerFactory("FreeBird");
em = emf.createEntityManager();
FacesContext context = FacesContext.getCurrentInstance();
HttpSession session = (HttpSession) context.getExternalContext().getSession(true);
User user = (User) session.getAttribute("frienddet");
Query query = em.createQuery("SELECT s FROM Status s WHERE s.email='"
+ user.getEmail() + "' ORDER BY s.timeMillis desc",
Status.class);
// other codes
}