我有一个 servlet 向其传递对象(集合)的 jsp。该对象成为会话属性:
session.setAttribute("groupList", request.getAttribute("groups"));
我使用一个标签从这个集合的内容中递归地生成一个菜单:
<c:forEach items="${groupList}" var="emp" begin="0" end="10">
<tag:menuRenderer groupList="${groupList}" />
</c:forEach>
中的那些项目是一个类的对象Group
。这个类包含一个子组subGroups
列表和一个名为 的不同类的对象列表students
。
我的问题是我无法在我的标签中访问这个学生列表。这是标签代码:
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib tagdir="/WEB-INF/tags" prefix="tag"%>
<%@ taglib uri='http://java.sun.com/jsp/jstl/functions' prefix='fn' %>
<%@ attribute name="groupList" type="java.util.Collection"%>
<c:forEach var="item" items="${groupList}">
<li><c:out value="${item.name}" /></li>
<br>
<!-- ##### here #### -->
<li><c:out value="${fn:length(item.students)}" /> <!-- #### doesn't work ####-->
<li><c:out value="${fn:length(item.subGroups)}" /> <!-- #### works #### -->
<ul>
<tag:menuRenderer groupList="${item.subGroups}" />
</ul>
</c:forEach>
调用会item.students
导致异常,但我看不到它到底是什么,因为 strack 跟踪仅显示在 jsp 中引发异常的位置:
SEVERE: Servlet.service() for servlet jsp threw exception
org.hibernate.LazyInitializationException: could not initialize proxy - no Session
at org.hibernate.collection.internal.AbstractPersistentCollection.withTemporarySessionIfNeeded(AbstractPersistentCollection.java:186)
at org.hibernate.collection.internal.AbstractPersistentCollection.readSize(AbstractPersistentCollection.java:137)
at org.hibernate.collection.internal.PersistentSet.size(PersistentSet.java:156)
at org.apache.taglibs.standard.functions.Functions.length(Functions.java:202)
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 org.apache.el.parser.AstFunction.getValue(AstFunction.java:112)
at org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:185)
at org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate(PageContextImpl.java:986)
at org.apache.jsp.tag.webmenuRenderer_tag._jspx_meth_c_005fout_005f1(webmenuRenderer_tag.java:202)
at org.apache.jsp.tag.webmenuRenderer_tag._jspx_meth_c_005fforEach_005f0(webmenuRenderer_tag.java:143)
at org.apache.jsp.tag.webmenuRenderer_tag.doTag(webmenuRenderer_tag.java:97)
at org.apache.jsp.tag.webmenuRenderer_tag._jspx_meth_tag_005fmenuRenderer_005f0(webmenuRenderer_tag.java:240)
at org.apache.jsp.tag.webmenuRenderer_tag._jspx_meth_c_005fforEach_005f0(webmenuRenderer_tag.java:153)
at org.apache.jsp.tag.webmenuRenderer_tag.doTag(webmenuRenderer_tag.java:97)
at org.apache.jsp.testMenu2_jsp._jspx_meth_tag_005fmenuRenderer_005f0(testMenu2_jsp.java:173)
at org.apache.jsp.testMenu2_jsp._jspx_meth_c_005fforEach_005f0(testMenu2_jsp.java:138)
at org.apache.jsp.testMenu2_jsp._jspService(testMenu2_jsp.java:90)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:749)
at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:487)
at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:412)
at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:339)
at myPackage.controller.ControllerServlet.doGet(ControllerServlet.java:38)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
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:99)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:929)
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:1002)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:585)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Nov 29, 2012 5:50:36 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [Hello] in context with path [/HibernateTest] threw exception [An exception occurred processing JSP page /testMenu2.jsp at line 19
16:
17: <ul>
18: <c:forEach items="${treeMap}" var="emp" begin="0" end="10">
19: <tag:menuRenderer treeMap="${treeMap}" />
20: </c:forEach>
21: </ul>
22:
Stacktrace:] with root cause
org.hibernate.LazyInitializationException: could not initialize proxy - no Session
at org.hibernate.collection.internal.AbstractPersistentCollection.withTemporarySessionIfNeeded(AbstractPersistentCollection.java:186)
at org.hibernate.collection.internal.AbstractPersistentCollection.readSize(AbstractPersistentCollection.java:137)
at org.hibernate.collection.internal.PersistentSet.size(PersistentSet.java:156)
at org.apache.taglibs.standard.functions.Functions.length(Functions.java:202)
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 org.apache.el.parser.AstFunction.getValue(AstFunction.java:112)
at org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:185)
at org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate(PageContextImpl.java:986)
at org.apache.jsp.tag.webmenuRenderer_tag._jspx_meth_c_005fout_005f1(webmenuRenderer_tag.java:202)
at org.apache.jsp.tag.webmenuRenderer_tag._jspx_meth_c_005fforEach_005f0(webmenuRenderer_tag.java:143)
at org.apache.jsp.tag.webmenuRenderer_tag.doTag(webmenuRenderer_tag.java:97)
at org.apache.jsp.tag.webmenuRenderer_tag._jspx_meth_tag_005fmenuRenderer_005f0(webmenuRenderer_tag.java:240)
at org.apache.jsp.tag.webmenuRenderer_tag._jspx_meth_c_005fforEach_005f0(webmenuRenderer_tag.java:153)
at org.apache.jsp.tag.webmenuRenderer_tag.doTag(webmenuRenderer_tag.java:97)
at org.apache.jsp.testMenu2_jsp._jspx_meth_tag_005fmenuRenderer_005f0(testMenu2_jsp.java:173)
at org.apache.jsp.testMenu2_jsp._jspx_meth_c_005fforEach_005f0(testMenu2_jsp.java:138)
at org.apache.jsp.testMenu2_jsp._jspService(testMenu2_jsp.java:90)
// and so on
如果有人能告诉我为什么这不起作用,我会很高兴!我必须导入一些东西吗?
提前致谢!:)