我在 Home.xhtml 上使用延迟滚动。问题是,每当我向下滚动到页面底部时,JavascriptgetMoreStatusList
都会不停地调用 getMoreStatusList.xhtml 页面上的函数,直到编译器抛出索引越界异常。我怎样才能解决这个问题?
1)主页.xhtml
<h:head>
<script type="text/javascript" src="jquery-1.4.2.min.js"></script>
<script type="text/javascript">
/* <![CDATA[ */
$(document).ready(function() {
function lastAddedLiveFunc() {
$('div#lastPostsLoader').html('<img src="bigLoader.gif">');
$.get("getMoreStatusList.xhtml", function(data) {
if (data != "") {
//console.log('add data..');
$(".items").append(data);
}
$('div#lastPostsLoader').empty();
});
};
//lastAddedLiveFunc();
$(window).scroll(function(){
var wintop = $(window).scrollTop(), docheight = $(document).height(), winheight = $(window).height();
var scrolltrigger = 0.95;
if ((wintop/(docheight-winheight)) > scrolltrigger) {
//console.log('scroll bottom');
lastAddedLiveFunc();
}
});
});
/* ]]> */
</script>
</h:head>
<h:body class="thrColElsHdr">
<!-- -->
<div class="items">
<div id="scroll_items">
<c:forEach var="p" items="#{statusBean.statusList}">
<h:form>
<div class="status">
<c:if test="${p.statusmsg!=null}">
<!-- for text status msg check-->
<h:commandLink
action="#{friendBean.gotoFriendProfile(p.email)}"
styleClass="link">
<img src="../images/profilePicture/thumb/#{p.picture}"
style="height: 39px; width: 39px;" /> <h:outputText
value="#{p.statusBy}:" />
</h:commandLink>
<br />
</c:if>
</c:forEach>
</div>
</div>
</h:body>
</html>
2) 状态 Bean(包含 getMoreStatusList 函数)
public class StatusBean {
public List<Status> getStatusList() {
FacesContext context = FacesContext.getCurrentInstance();
HttpSession session = (HttpSession) context.getExternalContext()
.getSession(true);
User user = (User) session.getAttribute("userdet");
Query query = em.createQuery("SELECT s FROM Status s WHERE s.email='"
+ user.getEmail() + "' ORDER BY s.timeMillis desc",
Status.class);
List<Status> results = query.getResultList();
Collections.sort(results);
results = results.subList(0, 5);
session.setAttribute("statusindex", 5);
return results;
}
public List<Status> getMoreStatusList() {
System.out.println("Inside getMoreStatusList");
FacesContext context = FacesContext.getCurrentInstance();
HttpSession session = (HttpSession) context.getExternalContext()
.getSession(false);
User user = (User) session.getAttribute("userdet");
Query query = em.createQuery("SELECT s FROM Status s WHERE s.email='"
+ user.getEmail() + "' ORDER BY s.timeMillis desc",
Status.class);
List<Status> results = query.getResultList();
Collections.sort(results);
int index = (int) session.getAttribute("statusindex");
System.out.println(index);
results = results.subList(index,index+5);
session.setAttribute("statusindex", index + 5);
return results;
}
}
3) 获取更多状态列表.xhtml
<h:head></h:head>
<h:body>
<c:forEach var="p" items="#{statusBean.moreStatusList}">
<h:form>
<div class="status">
<c:if test="${p.statusmsg!=null}">
<!-- for text status msg check-->
<h:commandLink action="#{friendBean.gotoFriendProfile(p.email)}"
styleClass="link">
<img src="../images/profilePicture/thumb/#{p.picture}"
style="height: 39px; width: 39px;" /> <h:outputText
value="#{p.statusBy}:" />
</h:commandLink>
<h:outputText value="#{p.statusmsg}" styleClass="textstyle1" />
<h:outputText value="#{p.timeMillis}"
style="font-size:xx-small;float:right;color:#bbbbbb;font-style: italic;">
<f:converter converterId="timeConverter" />
</div>
</c:forEach>
<br />
<div class="comment">
<p:inputText value="#{statusBean.comment.comment}"
styleClass="box" />
<p:commandLink value="Views"
action="#{statusBean.update(p.statusId)}" ajax="false"
styleClass="link" />
</div>
<br />
</c:if>
</c:forEach>
</h:body>
</html>