Spring项目实践(二)---web.xml文件详解

时间:2023-02-03 10:07:26

上一篇讲了spring项目中的pom.xml文件,pom文件主要作用是引入依赖库,设置编译参数等
这一篇来讲一下web.xml文件。
web.xml的学名叫做部署描述文件(DD),它不是Spring所特有的,而是在Servlet规范中定义的,是web应用的配置文件。
我们还是按照之前的套路,一部分一部分的讲解。

一.根标签
根标签是<web-app>,这块比较简单,模板里也自带,不再赘述

二.上下文配置Context

    <context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/spring-*.xml
</param-value>
</context-param>

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

context-param标签并不是专门为spring所设计的。在servlet中,context-param标签用来配置上下文参数,这个参数在整个web应用中都是可以访问的。
同样,listener也是servlet的概念。这个listener主要用来监视servlet的生命周期。

而在上面的例子中,contextConfigLocation这个上下文参数用来指定spring的配置文件,ContextLoaderListener则用来监视spring的生命周期。

具体的我们可以看一下spring的源码:

//可以看到,ContextLoaderListener继承自ServletContextListener
public class ContextLoaderListener extends ContextLoader implements ServletContextListener {

private ContextLoader contextLoader;
/**
* Create a new {@code ContextLoaderListener} that will create a web application
* context based on the "contextClass" and "contextConfigLocation" servlet
* context-params. See {@link ContextLoader} superclass documentation for details on
* default values for each.
* <p>This constructor is typically used when declaring {@code ContextLoaderListener}
* as a {@code <listener>} within {@code web.xml}, where a no-arg constructor is
* required.
* <p>The created application context will be registered into the ServletContext under
* the attribute name {@link WebApplicationContext#ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE}
* and the Spring application context will be closed when the {@link #contextDestroyed}
* lifecycle method is invoked on this listener.
* @see ContextLoader
* @see #ContextLoaderListener(WebApplicationContext)
* @see #contextInitialized(ServletContextEvent)
* @see #contextDestroyed(ServletContextEvent)
*/

public ContextLoaderListener() {
}

关键注释:

Create a new {@code ContextLoaderListener} that will create a web application
context based on the “contextClass” and “contextConfigLocation” servlet
context-params. See {@link ContextLoader} superclass documentation for details on
default values for each.

再看下上面提到的ContextLoader(截取部分代码):

public class ContextLoader {

/**
* Config param for the root WebApplicationContext implementation class to use: {@value}
* @see #determineContextClass(ServletContext)
* @see #createWebApplicationContext(ServletContext, ApplicationContext)
*/

public static final String CONTEXT_CLASS_PARAM = "contextClass";


/**
* Name of servlet context parameter (i.e., {@value}) that can specify the
* config location for the root context, falling back to the implementation's
* default otherwise.
* @see org.springframework.web.context.support.XmlWebApplicationContext#DEFAULT_CONFIG_LOCATION
*/

public static final String CONFIG_LOCATION_PARAM = "contextConfigLocation";

可以看到默认的CONFIG_LOCATION_PARAM参数值就是contextConfigLocation,而后面的方法:

        wac.setServletContext(sc);
String initParameter = sc.getInitParameter(CONFIG_LOCATION_PARAM);
if (initParameter != null) {
wac.setConfigLocation(initParameter);
}

则是直接将contextConfigLocation作为参数来配置。

三.springmvc的配置(实际上是servlet的配置)

    <servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/springmvc-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

这里可以看到,说是springmvc的配置,其实就是一个servlet的配置,不过servlet的名字叫springmvc
其实这个servlet的名字叫什么无所谓,你可以叫servlet123,关键是servlet-class。
servlet-class的值是DispatcherServlet,它是springmvc的一个前端控制器,它负责:
1、文件上传解析,如果请求类型是multipart将通过MultipartResolver进行文件上传解析;
2、通过HandlerMapping,将请求映射到处理器(返回一个HandlerExecutionChain,它包括一个处理器、多个HandlerInterceptor拦截器);
3、通过HandlerAdapter支持多种类型的处理器(HandlerExecutionChain中的处理器);
4、通过ViewResolver解析逻辑视图名到具体视图实现;
5、本地化解析;
6、渲染具体的视图等;
7、如果执行过程中遇到异常将交给HandlerExceptionResolver来解析。

另外说一下几个参数的意思:
contextConfigLocation这个参数可以不配置,如果不配置的话,那么默认的value就是/WEB-INF/[servlet名字]-servlet.xml,也就是说上面的不配置跟配置没区别。
load-on-startup表示启动容器时初始化该Servlet,这个参数没有默认值,如果不配置,或者当这个值小于0的时候,表示请求该servlet时才会初始化。另外当它大于等于0时,值越小,该servlet的优先级越高,应用启动时就越先加载。
servlet-mapping是servlet跟url之间的一个映射关系。比如上面的例子,实际上创建了一个springmvc这个servlet跟url为:yourWebLocation/web/的一个映射。比如你的域名是myweb.com,那么url-pattern
这里的绝对路径其实是myweb.com/web/。
这个映射有什么意义呢?它表示servlet会处理所有符合url规则的请求。在我们的例子里,表示springmvc这个servlet会处理所有来自你的网站的请求(因为映射到了根目录)

PS:这里有个小知识点,url-pattern的值,/跟/*有什么区别呢?
/和/*的区别:
< url-pattern > / < / url-pattern > 不会匹配到*.jsp,即:*.jsp不会进入spring的 DispatcherServlet类 。
< url-pattern > /* < / url-pattern > 会匹配*.jsp,会出现返回jsp视图时再次进入spring的DispatcherServlet 类,导致找不到对应的controller所以报404错。

四. filter过滤器的配置
过滤器可以看做是AOP的一种实现,可以对某一类的请求进行截获并添加一些额外的处理

<filter>
<filter-name>encodingFilter</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>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>

<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>


<filter>
<filter-name>shiroFilter</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
<init-param>
<param-name>targetFilterLifecycle</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>shiroFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

上面是我们比较常用的几个过滤器。
encodingFilter看字面意思就知道,是一个编码过滤器,它会截获所有的请求,并将他们统一转化成utf-8(通过我们设置的encoding参数)
shiroFilter是shiro安全框架的过滤器,我们通过过滤所有请求,可以对不同请求附加不同的角色和权限要求

比较简单的项目,web.xml配置就这些了,结束。