【❤️终极解决方案❤️】Could not open servletContext resource [/WEB-INF/dispatcher-servlet.xml]
java.io.FileNotFoundException: Could not open ServletContext resource [/WEB-INF/dispatcher-servlet.xml] org.springframework.web.context.support.ServletContextResource.getInputStream(ServletContextResource.java:159) org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:333) org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:310) org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:188) org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:224) org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:195) org.springframework.web.context.support.XmlWebApplicationContext.loadBeanDefinitions(XmlWebApplicationContext.java:125) org.springframework.web.context.support.XmlWebApplicationContext.loadBeanDefinitions(XmlWebApplicationContext.java:94) org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:130) org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:671) org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:553) org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:702) org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:668) org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:716) org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:591) org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:530) org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:170) javax.servlet.GenericServlet.init(GenericServlet.java:158) org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:541) org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92) org.apache.catalina.valves.AbstractA***essLogValve.invoke(AbstractA***essLogValve.java:687) org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:360) org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:399) org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65) org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:890) org.apache.tomcat.util.***.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1743) org.apache.tomcat.util.***.SocketProcessorBase.run(SocketProcessorBase.java:49) org.apache.tomcat.util.threads.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1191) org.apache.tomcat.util.threads.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:659) org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) java.lang.Thread.run(Thread.java:748)
1、导致原因:
如同上面说的,启动Tomcat之后,项目初始化找不到配置文件dispatcher-servlet.xml。
1.1、配置文件命名问题
-
web.xml文件中配置的servlet名字和springmvc配置文件的名字不一样,修改成一样即可。
这里要注意的是:的名字默认为servlet-name 的值-servlet.xml,所以我们需要删除第一张图中dispatcher-servlet后面的“-servlet”部分,因为这个部分是默认就有的。
-
springmvc的配置文件是:
dispatcher-servlet.xml
-
servlet-name配置是:
<servlet>
<servlet-name>dispatcher</servlet-name>//这里不能写成dispatcher-servlet,因为 -servlet是默认就有的!!!
<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>
1.2、Spring容器启动问题
-
Spring容器启动的问题,没有配置好启动的spring配置文件。
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
这个文件的映射配置是必须的,而且命名不能自定义,必修是 <param-name>contextConfigLocation</param-name>
2、参考文章
- 《关于“Could not open ServletContext resource [/WEB-INF/applicationContext.xml]”解决方案》
- 《解决 Could not open ServletContext resource [/WEB-INF/core-servlet.xml]异常》
- 《初学springMVC报错–Servlet.init() for servlet DispatcherServlet threw exception》