springMVC的配置文件放在哪里?

版权声明:本文为博主原创文章,未经博主允许不得转载。你想转载请附加连接哦 https://blog.csdn.net/dmw412724/article/details/81155906

今天重新在电脑上部署一个SpringMVC的web项目,但是很久没有手动配置, 就只记得DispatcherServlet
  <servlet>
      <servlet-name>DispatcherServlet</servlet-name>
      <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  </servlet>
  <servlet-mapping>
      <servlet-name>DispatcherServlet</servlet-name>
      <url-pattern>/</url-pattern>
  </servlet-mapping>

忘记里SpringMVC的XML怎么配置了,便没有配置,立即运行,没有报错,但是一旦进行访问,报错如下:
 IOException parsing XML document from ServletContext resource [/WEB-INF/DispatcherServlet-servlet.xml]; nested exception is java.io.FileNotFoundException: Could not open ServletContext resource [/WEB-INF/DispatcherServlet-servlet.xml]

   这说明SpringMVC自动会默认去找/WEB-INF/DispatcherServlet-servlet.xml这个XML.那么肯定是我忘了传递自配置的
xml路径导致的问题.
这时查看报错信息,发现里DispatcherServlet的父类也在报错信息里面,且包括init字样,便打开里该类的typeTree
发现里一个属性:contextConfigLocation,觉得有些熟悉,便尝试一番:
<servlet>
      <servlet-name>DispatcherServlet</servlet-name>
      <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
      <init-param>
          <param-name>contextConfigLocation</param-name>
          <param-value>classpath:aaa.xml</param-value>
      </init-param>
  </servlet>
  <servlet-mapping>
      <servlet-name>DispatcherServlet</servlet-name>
      <url-pattern>/</url-pattern>
  </servlet-mapping>

  这时候再打开请求就是报错:
org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [aaa.xml]; nested exception is java.io.FileNotFoundException: class path resource [aaa.xml] cannot be opened because it does not exist
说明有效果了,就是这个参数.

展开阅读全文

没有更多推荐了,返回首页