<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring.xml</param-value>
</context-param>
<!--Servlet that dispatches requests to the Spring managed block servlets-->
<servlet>
<description>springDispatcherServlet</description>
<display-name>DispatcherServlet</display-name>
<servlet-name>DispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--servlet的初始化参数-->
<init-param>
<!--contextConfigLocation:指定SpringMVC配置文件位置-->
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<!--servlet启动加载项,servlet原本是第一次访问创建对象,
load-on-startup就会在服务器启动的时候创建对象,并且值越小,优先级越高,越先创建对象。-->
<load-on-startup>1</load-on-startup>
</servlet>
<!-- /* 和 / 都是拦截所有请求,但
/*的范围更大,还会拦截到*.jsp页面,一旦拦截,页面就没办法显示了
/会拦截所有请求,除开jsp,能保证jsp正常显示-->
<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!--解决中文乱码的过滤器-->
<filter>
<filter-name>characterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
</web-app>
1、/*和/的区别
- / : 代表拦截所有请求,不拦截jsp页面,*.jsp请求;
- / * :拦截所有请求,但是,会拦截jsp页面,*.jsp请求;
2、设置/后无法访问.jsp外的静态资源
*.jsp是tomcat做的事;所有的小web.xml(项目里的web.xml)都是继承于大web.xml的:
对于html文件,tomcat视为静态资源,tomcat就会在服务器下找到这个资源并返回。
1)服务器的大web.xml中有一个DefaultServlet是url-pattern=/;
DefaultServlet是tomcat中处理静态资源的(除过jsp和servlet外,剩下的都是静态资源)
2)我们的配置中前端控制器url-pattern也是“/”;
这里我们自己的小web.xml“重写”了,相当于我们的前端控制器禁用了tomcat服务器中的DefaultServlet,这时候html(静态资源)就会来到我们配置的
DispatcherServlet(前端控制器),看那个方法的RequestMapping是这个,并没有那个方法返回是*.html,所以就不能访问到html
3)为什么jsp有可以访问:
因为我们没有覆盖服务器中大web.xml的JspServlet配置
4)我们写 / 也是迎合后来的Rest风格的URL地址