1

以下是我配置的配置文件

WEB-INF/classes/applicationContext.xml 但它正在搜索 WEB-INF/applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="starter_anil" 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>Anil-Spring</display-name>
    <!-- Servlets -->
    <servlet>
        <servlet-name>mvc-dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>WEB-INF/classes/applicationContext.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>mvc-dispatcher</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>

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

我得到以下异常

EVERE: Context initialization failed
org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from ServletContext resource [/WEB-INF/applicationContext.xml]; nested exception is java.io.FileNotFoundException: Could not open ServletContext resource [/WEB-INF/applicationContext.xml]

这是我的应用程序上下文

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

<beans>
    <import resource="config/controllers.xml" />
    <!-- <import resource="conf/spring/services.xml" />
    <import resource="conf/spring/persistence.xml" />
    <import resource="conf/spring/daos.xml" />
    <import resource="conf/spring//interceptors.xml" /> -->
</beans>

解决方案是

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="starter_anil" 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>Anil-Spring</display-name>
    <!-- Servlets -->
    <servlet>
        <servlet-name>mvc-dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/classes/applicationContext.xml</param-value>
        </init-param> 
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>mvc-dispatcher</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/classes/applicationContext.xml</param-value>
    </context-param>

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

警告:在名称为“mvc-dispatcher”的 DispatcherServlet 中未找到带有 URI [/AnilsSpring/WEB-INF/jsp/hello.jsp] 的 HTTP 请求的映射,请指导我一次

4

6 回答 6

1

你为什么applicationContext在课堂上?将其移至 WEB-INF 目录,如评论中所述。那应该可以解决问题。

/classes 目录用于存放已编译的类,但不用于配置文件。

于 2013-05-21T13:29:43.417 回答
0
<?xml version="1.0" encoding="UTF-8"?>

<web-app id="starter_anil" 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>Anil-Spring</display-name>
        <!-- Servlets -->
   <servlet>
        <servlet-name>mvc-dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/classes/applicationContext.xml</param-value>
       </init-param> 
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>mvc-dispatcher</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/classes/applicationContext.xml</param-value>
    </context-param>



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





</web-app>

这使得在 tomcat 上的异常减少,但我有点困惑

于 2013-05-21T13:53:38.040 回答
0

可能是您需要取消部署。并部署它。它会起作用的。. 它适用于从 ServletContext 资源 [/WEB-INF/classes/spring application config.xml] 解析 XML 文档的异常 IOException;

于 2013-12-04T15:06:07.550 回答
0

尝试添加/一个/WEB-INF/classes/applicationContext.xml

如果它仍然不起作用,请尝试将其添加到您的web.xml(在 servlet 声明之前)

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        /WEB-INF/classes/applicationContext.xml
    </param-value>
</context-param>
于 2013-05-21T13:30:00.160 回答
0

如果那是您绝对必须拥有的地方,请尝试以下操作:

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="starter_anil" 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>Anil-Spring</display-name>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value> 
    </context-param>

    <the-rest-of-your-stuff />
</web-app>

您可能需要/before applicationContext.xml,但我不这么认为。

于 2013-05-21T15:34:02.170 回答
-1

WEB-INF/classes/applicationContext.xml 对于这个要求,你必须把 applicationContext.xml 放在类路径中,即

如果您使用的是 maven,则将其放入 src/main/resources 文件夹,否则放入 src 文件夹。

然后你会在 WEB-INF/classes 里面找到它

于 2013-05-21T13:34:15.617 回答