在默认情况下:springmvc框架的配置文件必须叫<servlet-name>-servlet.xml
且必须放在/WEB-INF/目录下,我们可以在web.xml文件中,为DispatcherServlet配置一个初始化参数,
让它去我们指定的目录下加载springmvc.xml配置文件,web.xml代码如下:
<!-- 注册springmvc框架核心控制器 --> <servlet> <servlet-name>DispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/classes/loaderman/javaee/springmvc/config/springmvc.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>DispatcherServlet</servlet-name> <url-pattern>*.action</url-pattern> </servlet-mapping>
如果springmvc.xml配置文件放在src目录下,web.xml代码如下:
<!-- 注册springmvc框架核心控制器 --> <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:spring.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>DispatcherServlet</servlet-name> <url-pattern>*.action</url-pattern> </servlet-mapping>