如何将 Java 对象从 Servlet 传递(正确)到 JSP 页面
我在网络上到处都是,但我有点迷失,无法解决我的问题。
我正在使用 Eclipse 创建一个 Web 应用程序,在客户端使用 JSP,在服务器端使用 Servet/Hibernate。 我正在尝试将对象从 servlet 传递到 JSP 页面。
在 servlet 中:
Contact c = dao.getContact(dataID);
request.setAttribute("data", c);
getServletContext()
.getRequestDispatcher("/"+url+"?id="+dataID).forward(request, response);
在 JSP 页面中:
Contact contact = (Contact)request.getAttribute("data");
Contact 是一个 ORM,它具有“lastname”等属性。
但我收到此错误消息:
GRAVE: "Servlet.service()" pour la servlet GetData a généré une exception
org.hibernate.HibernateException: No CurrentSessionContext configured!
at org.hibernate.impl.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:683)
at domain.DAOContact.getContact(DAOContact.java:39)
at domain.GetData.doGet(GetData.java:27)
所以我试图找出原因,并且在某处读到我需要在休眠配置文件中将当前会话上下文设置为“线程”。但是当我这样做时,我收到此消息:
GRAVE: Servlet.service() for servlet [GetData] in context with path [/CarnetContacts] threw exception [org.hibernate.LazyInitializationException: could not initialize proxy - no Session] with root cause
org.hibernate.LazyInitializationException: could not initialize proxy - no Session
at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:167)
at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:215)
at org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.invoke(JavassistLazyInitializer.java:190)
at domain.Contact_$$_javassist_1.getLastName(Contact_$$_javassist_1.java)
消息的第二行表明它与延迟加载有关,但最后一行我显示当程序尝试获取“lastname”属性时会发生错误,该属性基本上是一个字符串,所以我不认为它是“延迟加载”。
因此,如果有人能帮助我找到解决方案,那就太好了。
dao方法的代码:
public Contact getContact(int contactId){
Session session = HibernateSessionFactory.getSessionFactory().getCurrentSession();
Transaction t = session.beginTransaction();
t.begin();
Contact contact =(Contact) session.load(Contact.class, new Integer(contactId));
t.commit();
return contact;
}
I've been everywhere on the web, and i'm little lost and couldn't solve my problem.
I'm creating a web application using Eclipse, with JSP on the client side, and Servet/Hibernate on the server side.
I'm trying to pass an Object to a JSP page form a servlet.
In the servlet :
Contact c = dao.getContact(dataID);
request.setAttribute("data", c);
getServletContext()
.getRequestDispatcher("/"+url+"?id="+dataID).forward(request, response);
In the JSP page :
Contact contact = (Contact)request.getAttribute("data");
Contact is an ORM, which has attributes like "lastname".
But i'm getting this error message :
GRAVE: "Servlet.service()" pour la servlet GetData a généré une exception
org.hibernate.HibernateException: No CurrentSessionContext configured!
at org.hibernate.impl.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:683)
at domain.DAOContact.getContact(DAOContact.java:39)
at domain.GetData.doGet(GetData.java:27)
So I tried to figure out why, and somewhere I read that I need to set the current session context to "thread" in the hibernate config file. But when I did this, i get this message :
GRAVE: Servlet.service() for servlet [GetData] in context with path [/CarnetContacts] threw exception [org.hibernate.LazyInitializationException: could not initialize proxy - no Session] with root cause
org.hibernate.LazyInitializationException: could not initialize proxy - no Session
at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:167)
at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:215)
at org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.invoke(JavassistLazyInitializer.java:190)
at domain.Contact_$_javassist_1.getLastName(Contact_$_javassist_1.java)
The second line of the message indicates it has to do with lazy loading, but the last line i shows that the bug occurs when the program is trying to get the "lastname" attribute, which is basically a string, so I don't think it's "lazy loaded".
So if somebody could help me find a solution, that would be very kind.
Code of the dao method :
public Contact getContact(int contactId){
Session session = HibernateSessionFactory.getSessionFactory().getCurrentSession();
Transaction t = session.beginTransaction();
t.begin();
Contact contact =(Contact) session.load(Contact.class, new Integer(contactId));
t.commit();
return contact;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在这种情况下,您的具体问题是您使用
Session.load()
而不是Session.get()
。load()
返回一个延迟初始化的代理,因此仅应在特殊情况下使用它,大多数情况下您需要使用get()
。一般来说,除了这种特殊情况外,视图层中的延迟初始化问题可以使用以下方法之一来解决:
使用 在视图模式中打开会话
在渲染视图之前获取所有需要的数据(使用
JOIN FETCH
、Hibernate.initialize()
等)Your specific problem in this case is that you use
Session.load()
instead ofSession.get()
.load()
returns a lazily-initialized proxy, therefore it should be used only in special cases, in the most cases you need to useget()
.Generally speaking, besides this particular case, problem with lazy initialization in view layer can be solved using one of the following approaches:
Use Open Session in View pattern
Fetch all required data before rendering the view (using
JOIN FETCH
,Hibernate.initialize()
, etc)