更改dispatcher-servlet.xml的位置不起作用

时间:2016-04-30 22:30:24

标签: java spring spring-mvc

已经在web.xml中将调度程序servlet的位置定义为context-param中的[/WEB-INF/config/dispatcher-servlet.xml],这仍然是为什么它正在寻找[/ WEB-INF / dispatcher- servlet.xml中]。

当在[/WEB-INF/config/dispatcher-servlet.xml]以及[/WEB-INF/dispatcher-servlet.xml]中填充dipatcher-servlet时,这将起作用,然后才能正常工作。< / p>

它是如何以这种方式表现的?如果将其当前位置更改为[/WEB-INF/config/dispatcher-servlet.xml],为什么还要在此[/WEB-INF/dispatcher-servlet.xml]位置进行搜索?

的web.xml

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

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

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

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            /WEB-INF/config/dispatcher-servlet.xml,
            /WEB-INF/config/spring-security.xml,
            /WEB-INF/config/database-config.xml
        </param-value>
    </context-param>

错误:

May 01, 2016 3:55:49 AM org.apache.catalina.core.ApplicationContext log
INFO: Initializing Spring FrameworkServlet 'dispatcher'
2016-05-01 03:55:49 INFO  DispatcherServlet:488 - FrameworkServlet 'dispatcher': initialization started
2016-05-01 03:55:49 INFO  XmlWebApplicationContext:578 - Refreshing WebApplicationContext for namespace 'dispatcher-servlet': startup date [Sun May 01 03:55:49 IST 2016]; parent: Root WebApplicationContext
2016-05-01 03:55:49 INFO  XmlBeanDefinitionReader:317 - Loading XML bean definitions from ServletContext resource [/WEB-INF/dispatcher-servlet.xml]
May 01, 2016 3:55:49 AM org.apache.catalina.core.ApplicationContext log
SEVERE: StandardWrapper.Throwable
org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from ServletContext resource [/WEB-INF/dispatcher-servlet.xml]; nested exception is java.io.FileNotFoundException: Could not open ServletContext resource [/WEB-INF/dispatcher-servlet.xml]
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:344)
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:304)
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:181)

2 个答案:

答案 0 :(得分:0)

您已经像这样定义了Dispatcher servlet:

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

因此,默认情况下,它将在类路径中查找调度程序servlet文件(dispatcher-servlet.xml)(您的servlet名称由'-servlet.xml'附加)。

您已定义了dispatcher-servlet.xml,但已将其保留在/ WEB-INF / config /的位置下。因此,请为您的调度程序servlet xml文件提供此位置。以下配置应该适合您。

<servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>
        org.springframework.web.servlet.DispatcherServlet
    </servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            /WEB-INF/conf/dispatcher-servlet.xml
        </param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

答案 1 :(得分:0)

案例1:如果没有定义init-param或context-param,servlet名称是调度程序

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

=&gt;当web.xml加载时,它将默认加载/WEB-INF/dispatcher-servlet.xml。您不需要在init-param中定义它

案例2:当您想要更改dispatcher-servlet.xml的位置时 =&gt;你需要在init-param中提及它的路径。因为init-param加载了这个xml