gpt4 book ai didi

java - OpenSessionInViewFilter 找不到 WebApplicationContext

转载 作者:太空宇宙 更新时间:2023-11-04 07:26:22 25 4
gpt4 key购买 nike

我知道配置 OSIVF 是一个常见的痛点。我已经阅读了过去几天能找到的所有页面,但似乎没有什么能让我解决这个问题。我已经成功地让自己感到困惑了。我试图保持这个配置非常简单,因为网络应用程序非常简单。嗯,错误如下:

java.lang.IllegalStateException: No WebApplicationContext found: no ContextLoaderListener registered?
org.springframework.web.context.support.WebApplicationContextUtils.getRequiredWebApplicationContext(WebApplicationContextUtils.java:90)
org.springframework.orm.hibernate4.support.OpenSessionInViewFilter.lookupSessionFactory(OpenSessionInViewFilter.java:190)

这是 web.xml:

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

<display-name>Test MVC Application</display-name>

<servlet>
<servlet-name>onepic</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>onepic</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

<filter>
<filter-name>hibernateFilter</filter-name>
<filter-class>org.springframework.orm.hibernate4.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>



<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>


</web-app>

最后,这是 onepic-servlet.xml 中的一些内容:

<bean id="datasource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/onePic?characterEncoding=UTF-8"/>
<property name="username" value="onePic"/>
<property name="password" value="onePic"/>
</bean>

<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="datasource" />
<property name="packagesToScan" value="com.sandofamily.onePic"/>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect"> org.hibernate.dialect.H2Dialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>

<tx:annotation-driven/>

<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>

再次,我很抱歉再次提出这样一个常见问题,但我一定缺少一些将这一切联系在一起的知识。

最佳答案

您尚未在 web.xml 中配置您的 Context,因此当 Spring 尝试查找其 Context 时,您会收到此错误。

This显示如何使用 xml 文件配置 Spring,但您也可以使用基于 Java 配置来执行此操作。可以找一下here .

关于java - OpenSessionInViewFilter 找不到 WebApplicationContext,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18470868/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com