JSF中的OpenSessionInViewFilter + Redirect [英] OpenSessionInViewFilter +Redirect in JSF

查看:131
本文介绍了JSF中的OpenSessionInViewFilter + Redirect的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Spring和Hibernate上都有JSF 2.1应用程序.

I have JSF 2.1 application uisng Spring and Hibernate.

为了解决LazyLoading问题,我使用了OpenSessionInView Filter.

In order to solve LazyLoading problem I'm using OpenSessionInView Filter.

<filter>
    <filter-name>openSessionInViewFilter</filter-name>
    <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
</filter>
 <filter-mapping>
    <filter-name>openSessionInViewFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

一切正常,我的惰性类已启动,但是当我将导航规则与重定向一起使用时

Everything works fine my lazy classes are initiated, but when I'm using Navigation Rule with Redirect

 <navigation-rule>
   <from-view-id>/index.xhtml</from-view-id>
   <navigation-case>
       <from-outcome>fail</from-outcome>
       <to-view-id>/index.xhtml</to-view-id>
       <redirect />
   </navigation-case>
</navigation-rule>

我得到org.hibernate.LazyInitializationException: id,我删除了<redirect />标记,一切正常.

I'm getting org.hibernate.LazyInitializationException: id I remove <redirect /> tag everything works fine.

为什么?是因为创建了新的视图吗?那么,为什么我要进行新的Hibernate Session? 有什么解决方法吗?

Why? Is it because new View has been created? So why do I have new Hibernate Session? Is there any work around?

推荐答案

<redirect />是通过向浏览器返回301(或303)响应代码来实现的,从而完成当前请求并关闭当前请求(休眠)会话.

<redirect /> is implemented by returning 301 (or 303) response code to the browser, thereby completing current request and closing your current (Hibernate) session.

浏览器然后向提供的URL提交 new 请求,从而导致全新的(休眠)会话,您尚未初始化的实体未绑定到该会话.

Browser then submits a new request to the URL provided, resulting in brand new (Hibernate) session which your not-yet-initialized entities are not bound to.

您可能需要在重定向之前手动触发现有请求中的初始化(如果可能的话),或者在重定向之后再次重新获取所有必需的实体.

You will either need to manually trigger initialization in existing request prior to redirect (if possible) or re-fetch all the necessary entities again after the redirect.

这篇关于JSF中的OpenSessionInViewFilter + Redirect的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆