web.xml文件源码
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 唤醒Servlet -->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<!-- 减去上下文的路径 例如123.html -->
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
springmvc-servlet.xml
<?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"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- 使用扫描机制,扫描控制器类 -->
<!-- 在这里的作用相当于@ComponentScan("com")组件扫描 -->
<context:component-scan base-package="com"/>
<mvc:annotation-driven />
<!-- 使用resources过滤掉不需要dispatcher servlet的资源(即静态资源,如css、js、html、images)。
使用resources时,必须使用annotation-driven,不然resources元素会阻止任意控制器被调用。
-->
<!-- 允许css目录下所有文件可见 -->
<mvc:resources location="/html/" mapping="/html/**"></mvc:resources>
<!-- 配置视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
id="internalResourceViewResolver">
<!-- 前缀 -->
<!-- 这里的vaule所指的路径是需要跳转的路径 -->
<property name="prefix" value="/WEB-INF/jsp/" />
<!-- 后缀 -->
<property name="suffix" value=".jsp" />
</bean>
</beans>
<servlet>和<servlet-mapping>中的<servlet-name>springmvc</servlet-name>
保持一致
因为在<servlet>的DispatherServlet的接口的父类(FrameworkServlet)中
public static final String DEFAULT_NAMESPACE_SUFFIX = "-servlet";
它会寻找xxx-servlet.xml文件,这里的xxx在这里对应的就是<servlet-name>中的springmvc。
源码如下: