【❤️终极解决方案❤️】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.AbstractAccessLogValve.invoke(AbstractAccessLogValve.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.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1743) org.apache.tomcat.util.net.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配置文件。
文章来源:https://www.toymoban.com/news/detail-684631.html
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
这个文件的映射配置是必须的,而且命名不能自定义,必修是 <param-name>contextConfigLocation</param-name>
文章来源地址https://www.toymoban.com/news/detail-684631.html
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》
到了这里,关于【终极解决方案】Could not open ServletContext resource [/WEB-INF/dispatcher-servlet.xml]的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!