IDE:idea
主题:SpringMVC简单配置
问题报错:Could not open ServletContext resource [/WEB-INF/my-servlet.xml]
IOException parsing XML document from ServletContext resource [/WEB-INF/mvc-servlet.xml]; nested exception is java.io.FileNotFoundException: Could not open ServletContext resource [/WEB-INF/mvc-servlet.xml]
解决方法:
按Spring配置文件的命名规则修改配置文件的名字,即可以解决问题。
原来以为路径写错,改了几次都没用。
规则为"servlet名-servlt.xml"来命名。其实错误里已经是按命名规则在查找mvc-servlet.xml。按照规则修改为和这个同名即可以。
过程:
原先认为xml文件名可以自己设置,对应就可以,但还是出问题报错。按规则改通过,那就按规则先改。
contextConfigLocation
/WEB-INF/my-servlet.xml
附上原来的xml文件my-servlet.xml
简单的配置指定注入Bean时Spring要查找的包,视图解析器。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.spring5.controller"/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
web.xml文件如下:
`<?xml version="1.0" encoding="UTF-8"?>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>mvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/my-servlet.xml</param-value>
</context-param>
`
把配置文件按照命名规则修改
<servlet>
<servlet-name>mvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
已经定义servlet是mvc,按命名规则修改配置文件为mvc-servlet.xml。
同时修改:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/mvc-servlet.xml</param-value>
</context-param>
问题解决。
如果对你有帮助,留下脚印吧!以上仅是我学习过程记录。各位大神帮忙指正。