每当我运行xhtml时。它给了我以下例外。statusindex对象的值为5。我正在使用JQuery进行延迟滚动,因此当我的xhml页面getMoreStatusList调用getMoreStatusList函数时,它为我提供了超出范围的异常的索引。数据库中有26个状态更新,因此索引超出范围的异常对我来说毫无意义。
1)代码
public List<Status> 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(); Query query1 = em.createQuery("SELECT f FROM Friend f WHERE f.email='" + user.getEmail() + "'", Friend.class); List<Friend> results1 = query1.getResultList(); Iterator<Friend> it = results1.listIterator(); while (it.hasNext()) { String email = it.next().getFriendEmail(); Query query2 = em.createQuery( "SELECT s FROM Status s WHERE s.email='" + email + "' ORDER BY s.timeMillis desc", Status.class); List<Status> results2 = query2.getResultList(); results.addAll(results2); } 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; }
2)例外
Mar 02, 2013 9:31:58 PM org.apache.catalina.core.StandardWrapperValve invoke SEVERE: Servlet.service() for servlet [Faces Servlet] in context with path [/FreeBird_v.6] threw exception [/getMoreStatusList.xhtml @120,43 test="#{p.videostatus!=null}" /getMoreStatusList.xhtml @120,43 test="#{p.videostatus!=null}": /getMoreStatusList.xhtml @14,60 items="#{statusBean.moreStatusList}": Error reading 'moreStatusList' on type com.bean.StatusBean] with root cause java.lang.IndexOutOfBoundsException: toIndex = 30 at java.util.SubList.<init>(Unknown Source) at java.util.RandomAccessSubList.<init>(Unknown Source) at java.util.AbstractList.subList(Unknown Source) at java.util.Vector.subList(Unknown Source) at com.bean.StatusBean.getMoreStatusList(StatusBean.java:190) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at javax.el.BeanELResolver.getValue(BeanELResolver.java:87) at javax.el.CompositeELResolver.getValue(CompositeELResolver.java:67) at com.sun.faces.el.FacesCompositeELResolver.getValue(FacesCompositeELResolver.java:71) at org.apache.el.parser.AstValue.getValue(AstValue.java:169) at org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:189) at com.sun.faces.facelets.el.TagValueExpression.getValue(TagValueExpression.java:106) at com.sun.faces.facelets.tag.jstl.core.IndexedValueExpression.getValue(IndexedValueExpression.java:89) at org.apache.el.parser.AstIdentifier.getValue(AstIdentifier.java:68) at org.apache.el.parser.AstValue.getValue(AstValue.java:147) at org.apache.el.parser.AstNotEqual.getValue(AstNotEqual.java:38) at org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:189) at com.sun.faces.facelets.el.TagValueExpression.getValue(TagValueExpression.java:106) at com.sun.faces.facelets.tag.TagAttributeImpl.getObject(TagAttributeImpl.java:326) at com.sun.faces.facelets.tag.TagAttributeImpl.getBoolean(TagAttributeImpl.java:133) at com.sun.faces.facelets.tag.jstl.core.IfHandler.apply(IfHandler.java:88) at javax.faces.view.facelets.CompositeFaceletHandler.apply(CompositeFaceletHandler.java:94) at javax.faces.view.facelets.DelegatingMetaTagHandler.applyNextHandler(DelegatingMetaTagHandler.java:131) at com.sun.faces.facelets.tag.jsf.ComponentTagHandlerDelegateImpl.apply(ComponentTagHandlerDelegateImpl.java:162) at javax.faces.view.facelets.DelegatingMetaTagHandler.apply(DelegatingMetaTagHandler.java:114) at com.sun.faces.facelets.tag.jstl.core.ForEachHandler.apply(ForEachHandler.java:214) at javax.faces.view.facelets.DelegatingMetaTagHandler.applyNextHandler(DelegatingMetaTagHandler.java:131) at com.sun.faces.facelets.tag.jsf.ComponentTagHandlerDelegateImpl.apply(ComponentTagHandlerDelegateImpl.java:162) at javax.faces.view.facelets.DelegatingMetaTagHandler.apply(DelegatingMetaTagHandler.java:114) at javax.faces.view.facelets.CompositeFaceletHandler.apply(CompositeFaceletHandler.java:94) at com.sun.faces.facelets.compiler.NamespaceHandler.apply(NamespaceHandler.java:89) at javax.faces.view.facelets.CompositeFaceletHandler.apply(CompositeFaceletHandler.java:94) at com.sun.faces.facelets.compiler.EncodingHandler.apply(EncodingHandler.java:79) at com.sun.faces.facelets.impl.DefaultFacelet.apply(DefaultFacelet.java:148) at com.sun.faces.application.view.FaceletViewHandlingStrategy.buildView(FaceletViewHandlingStrategy.java:734) at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:96) at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:97) at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:135) at javax.faces.webapp.FacesServlet.service(FacesServlet.java:309) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) at org.primefaces.webapp.filter.FileUploadFilter.doFilter(FileUploadFilter.java:79) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:225) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123) at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98) at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:927) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407) at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1001) at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:579) at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312) at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) at java.lang.Thread.run(Unknown Source)
消息说toIndex是30。这statusindex不是您认为的5,而是25:
toIndex
statusindex
java.lang.IndexOutOfBoundsException: toIndex = 30
旁注,您应该在查询中使用命名参数,而不要使用String串联来传递参数。您的代码容易受到SQL注入攻击。如果电子邮件地址恰好包含单引号,它也会失败。