使用Spring 3和Servlet 3配置OpenSessionInViewFilter [英] Configuring OpenSessionInViewFilter with Spring 3 and Servlet 3

查看:124
本文介绍了使用Spring 3和Servlet 3配置OpenSessionInViewFilter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想配置 OpenSessionInViewFilter 能够在视图中使用hibernate惰性初始化,所以我在web.xml中添加了过滤器定义,但它仍然不起作用

 <?xml version =1.0encoding =UTF- 8\" >?; 

< web-app xmlns =http://java.sun.com/xml/ns/javaee
xmlns:xsi =http://www.w3.org / 2001 / XMLSchema-instance
xsi:schemaLocation =http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0 .xsd
version =3.0>

< display-name> MyApp< / display-name>

< listener>
< listener-class>
org.springframework.web.context.ContextLoaderListener
< / listener-class>
< / listener>

< listener>
< listener-class>
org.springframework.web.context.request.RequestContextListener
< / listener-class>
< / listener>

< context-param>
< param-name> contextConfigLocation< / param-name>
< param-value>
classpath:META-INF / spring / applicationContext.xml
classpath:META-INF / spring / applicationSecurity.xml
< / param-value>

< / context-param>

< context-param>
< param-name> javax.faces.FACELETS_LIBRARIES< / param-name>
< param-value> /WEB-INF/springsecurity.taglib.xml< / param-value>
< / context-param>


< context-param>
< param-name> javax.faces.PROJECT_STAGE< / param-name>
<参数值>开发< /参数值>
< / context-param>

< welcome-file-list>
< welcome-file> /< / welcome-file>
< / welcome-file-list>

< context-param>
< description>状态保存方法:'client'或'server'(= default)。请参阅JSF规范2.5.2< / description>
< param-name> javax.faces.STATE_SAVING_METHOD< / param-name>
< param-value>服务器< /参数值>
< / context-param>

< context-param>
< param-name> javax.servlet.jsp.jstl.fmt.localizationContext< / param-name>
< param-value> resources.application< / param-value>
< / context-param>


< listener>
< listener-class> com.sun.faces.config.ConfigureListener< / listener-class>
< / listener>

< context-param>
< param-name> javax.faces.FACELETS_SKIP_COMMENTS< / param-name>
< param-value> true< /参数值>
< / context-param>


< filter>
< filter-name> springSecurityFilterChain< / filter-name>
< filter-class>
org.springframework.web.filter.DelegatingFilterProxy
< / filter-class>
< / filter>

< filter-mapping>
< filter-name> springSecurityFilterChain< / filter-name>
< url-pattern> / *< / url-pattern>
< dispatcher> FORWARD< / dispatcher>
< dispatcher> REQUEST< / dispatcher>
< dispatcher> ERROR< / dispatcher>
< / filter-mapping>


< filter>
< filter-name>漂亮过滤器< /过滤器名称>
< filter-class> com.ocpsoft.pretty.PrettyFilter< / filter-class>
< init-param>
< param-name> logLevel< / param-name>
<参数值>错误< /参数值>
< / init-param>
< / filter>

< filter-mapping>
< filter-name>漂亮过滤器< /过滤器名称>
< url-pattern> / *< / url-pattern>
< dispatcher> FORWARD< / dispatcher>
< dispatcher> REQUEST< / dispatcher>
< dispatcher> ERROR< / dispatcher>
< / filter-mapping>

< filter>
< filter-name> hibernateFilter< / filter-name>
< filter-class> org.springframework.orm.hibernate3.support.OpenSessionInViewFilter< / filter-class>
< init-param>
< param-name> sessionFactoryBeanName< / param-name>
< param-value> sessionFactory< / param-value>
< / init-param>
< / filter>

< filter-mapping>
< filter-name> hibernateFilter< / filter-name>
< url-pattern> / *< / url-pattern>
< / filter-mapping>



< servlet>
< servlet-name>资源Servlet< / servlet-name>
< servlet-class> com.icesoft.faces.webapp.CompatResourceServlet< / servlet-class>
1< / load-on-startup>
< / servlet>

< servlet-mapping>
< servlet-name>资源Servlet< / servlet-name>
< url-pattern> / xmlhttp / *< / url-pattern>
< / servlet-mapping>

< servlet>
< servlet-name> Faces Servlet< / servlet-name>
< servlet-class> javax.faces.webapp.FacesServlet< / servlet-class>
1< / load-on-startup>
< / servlet>

< servlet-mapping>
< servlet-name> Faces Servlet< / servlet-name>
< url-pattern> / faces / *< / url-pattern>
< / servlet-mapping>

< / web-app>

applicationContext:

 < bean id =sessionFactory
class =org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean>

< property name =dataSourceref =dataSource/>
< property name =packagesToScanvalue =$ {project.groupId} .domain/>


<! - 控制Hibernate在运行时的行为,全部都是可选的,并且有合理的默认值 - >
< property name =hibernateProperties>
<值>
<! - hibernate.dialect:允许Hibernate生成针对特定关系数据库优化的SQL - >
hibernate.dialect = org.hibernate.dialect.PostgreSQLDialect
hibernate.hbm2ddl.auto = create-drop
hibernate.show_sql = true
hibernate.format_sql = true
< /值GT;
< / property>

< / bean>

请告诉我为什么我的仍然获得延迟初始化异常,谢谢前进。

解决方案

看起来,过滤器链的构建顺序取决于过滤器映射元素出现的顺序。
$ b 换句话说,尝试在其他过滤器映射之前放置OpenSessionInViewFilter的过滤器映射。



好祝你好运


i want to configure OpenSessionInViewFilter to able to use hibernate lazy initialization in view, so i added the filter definition in web.xml, but it doesn't work i still get the same lazy initialization exception, here's what i did:

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns="http://java.sun.com/xml/ns/javaee"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
      version="3.0"> 

  <display-name>MyApp</display-name>

  <listener>
    <listener-class>
        org.springframework.web.context.ContextLoaderListener
    </listener-class>
  </listener>

  <listener>
    <listener-class>
        org.springframework.web.context.request.RequestContextListener
    </listener-class>
  </listener>

  <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
        classpath:META-INF/spring/applicationContext.xml
        classpath:META-INF/spring/applicationSecurity.xml
        </param-value>

  </context-param>

  <context-param>
    <param-name>javax.faces.FACELETS_LIBRARIES</param-name>
    <param-value>/WEB-INF/springsecurity.taglib.xml</param-value>
  </context-param>


  <context-param>
    <param-name>javax.faces.PROJECT_STAGE</param-name>
    <param-value>Development</param-value>
  </context-param>

  <welcome-file-list>
    <welcome-file>/</welcome-file>
  </welcome-file-list>

  <context-param>
    <description>State saving method: 'client' or 'server' (=default). See JSF Specification 2.5.2</description>
    <param-name>javax.faces.STATE_SAVING_METHOD</param-name>
    <param-value>server</param-value>
  </context-param>

  <context-param>
    <param-name>javax.servlet.jsp.jstl.fmt.localizationContext</param-name>
    <param-value>resources.application</param-value>
  </context-param>


  <listener>
    <listener-class>com.sun.faces.config.ConfigureListener</listener-class>
  </listener>

  <context-param>
    <param-name>javax.faces.FACELETS_SKIP_COMMENTS</param-name>
    <param-value>true</param-value>
  </context-param>


    <filter>
        <filter-name>springSecurityFilterChain</filter-name>
        <filter-class>
            org.springframework.web.filter.DelegatingFilterProxy
        </filter-class>
    </filter>

    <filter-mapping>
        <filter-name>springSecurityFilterChain</filter-name>
        <url-pattern>/*</url-pattern>
        <dispatcher>FORWARD</dispatcher>
        <dispatcher>REQUEST</dispatcher>
        <dispatcher>ERROR</dispatcher>
    </filter-mapping>


    <filter>
      <filter-name>Pretty Filter</filter-name>
      <filter-class>com.ocpsoft.pretty.PrettyFilter</filter-class>
      <init-param>
            <param-name>logLevel</param-name>
            <param-value>ERROR</param-value>
      </init-param>
    </filter>

    <filter-mapping>
    <filter-name>Pretty Filter</filter-name>
      <url-pattern>/*</url-pattern>
      <dispatcher>FORWARD</dispatcher>
      <dispatcher>REQUEST</dispatcher>
      <dispatcher>ERROR</dispatcher>
    </filter-mapping>

    <filter>
      <filter-name>hibernateFilter</filter-name>
      <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
      <init-param>
         <param-name>sessionFactoryBeanName</param-name>
         <param-value>sessionFactory</param-value>         
      </init-param>      
   </filter>

   <filter-mapping>
     <filter-name>hibernateFilter</filter-name>
     <url-pattern>/*</url-pattern>
   </filter-mapping> 



  <servlet>
    <servlet-name>Resource Servlet</servlet-name>
    <servlet-class>com.icesoft.faces.webapp.CompatResourceServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>

  <servlet-mapping>
    <servlet-name>Resource Servlet</servlet-name>
    <url-pattern>/xmlhttp/*</url-pattern>
  </servlet-mapping>

  <servlet>
    <servlet-name>Faces Servlet</servlet-name>
    <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>

  <servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>/faces/*</url-pattern>
  </servlet-mapping>

</web-app>

applicationContext:

<bean id="sessionFactory"
        class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">

        <property name="dataSource" ref="dataSource" />
        <property name="packagesToScan" value="${project.groupId}.domain" />


        <!-- control the behavior of Hibernate at runtime,All are optional and have reasonable default values -->
        <property name="hibernateProperties">
            <value>
            <!-- hibernate.dialect: allows Hibernate to generate SQL optimized for a particular relational database -->
                hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
                hibernate.hbm2ddl.auto=create-drop
                hibernate.show_sql=true
                hibernate.format_sql=true
            </value>
        </property>

    </bean>

please advise why i am still getting the lazy initialization exception, thanks in advance.

解决方案

It seems that the order in which the filter chain is constructed depends on the order that the filter-mapping elements appear.

In other words, try putting filter-mapping of OpenSessionInViewFilter before any other filter-mapping.

Good luck with that

这篇关于使用Spring 3和Servlet 3配置OpenSessionInViewFilter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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