|
上一篇講了spring項(xiàng)目中的pom.xml文件,pom文件主要作用是引入依賴庫,設(shè)置編譯參數(shù)等
這一篇來講一下web.xml文件。
web.xml的學(xué)名叫做部署描述文件(DD),它不是Spring所特有的,而是在Servlet規(guī)范中定義的,是web應(yīng)用的配置文件。
我們還是按照之前的套路,一部分一部分的講解。
一.根標(biāo)簽
根標(biāo)簽是<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標(biāo)簽并不是專門為spring所設(shè)計(jì)的。在servlet中,context-param標(biāo)簽用來配置上下文參數(shù),這個參數(shù)在整個web應(yīng)用中都是可以訪問的。
同樣,listener也是servlet的概念。這個listener主要用來監(jiān)視servlet的生命周期。
而在上面的例子中,contextConfigLocation這個上下文參數(shù)用來指定spring的配置文件,ContextLoaderListener則用來監(jiān)視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() {
}
關(guān)鍵注釋:
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";
可以看到默認(rèn)的CONFIG_LOCATION_PARAM參數(shù)值就是contextConfigLocation,而后面的方法:
wac.setServletContext(sc);
String initParameter = sc.getInitParameter(CONFIG_LOCATION_PARAM);
if (initParameter != null) {
wac.setConfigLocation(initParameter);
}
則是直接將contextConfigLocation作為參數(shù)來配置。
三.springmvc的配置(實(shí)際上是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的配置,其實(shí)就是一個servlet的配置,不過servlet的名字叫springmvc
其實(shí)這個servlet的名字叫什么無所謂,你可以叫servlet123,關(guān)鍵是servlet-class。
servlet-class的值是DispatcherServlet,它是springmvc的一個前端控制器,它負(fù)責(zé):
1、文件上傳解析,如果請求類型是multipart將通過MultipartResolver進(jìn)行文件上傳解析;
2、通過HandlerMapping,將請求映射到處理器(返回一個HandlerExecutionChain,它包括一個處理器、多個HandlerInterceptor攔截器);
3、通過HandlerAdapter支持多種類型的處理器(HandlerExecutionChain中的處理器);
4、通過ViewResolver解析邏輯視圖名到具體視圖實(shí)現(xiàn);
5、本地化解析;
6、渲染具體的視圖等;
7、如果執(zhí)行過程中遇到異常將交給HandlerExceptionResolver來解析。
另外說一下幾個參數(shù)的意思:
contextConfigLocation這個參數(shù)可以不配置,如果不配置的話,那么默認(rèn)的value就是/WEB-INF/[servlet名字]-servlet.xml,也就是說上面的不配置跟配置沒區(qū)別。
load-on-startup表示啟動容器時(shí)初始化該Servlet,這個參數(shù)沒有默認(rèn)值,如果不配置,或者當(dāng)這個值小于0的時(shí)候,表示請求該servlet時(shí)才會初始化。另外當(dāng)它大于等于0時(shí),值越小,該servlet的優(yōu)先級越高,應(yīng)用啟動時(shí)就越先加載。
servlet-mapping是servlet跟url之間的一個映射關(guān)系。比如上面的例子,實(shí)際上創(chuàng)建了一個springmvc這個servlet跟url為:yourWebLocation/web/的一個映射。比如你的域名是myweb.com,那么url-pattern
這里的絕對路徑其實(shí)是myweb.com/web/。
這個映射有什么意義呢?它表示servlet會處理所有符合url規(guī)則的請求。在我們的例子里,表示springmvc這個servlet會處理所有來自你的網(wǎng)站的請求(因?yàn)橛成涞搅烁夸洠?/p>
PS:這里有個小知識點(diǎn),url-pattern的值,/跟/*有什么區(qū)別呢?
/和/*的區(qū)別:
< url-pattern > / < / url-pattern > 不會匹配到*.jsp,即:*.jsp不會進(jìn)入spring的 DispatcherServlet類 。
< url-pattern > /* < / url-pattern > 會匹配*.jsp,會出現(xiàn)返回jsp視圖時(shí)再次進(jìn)入spring的DispatcherServlet 類,導(dǎo)致找不到對應(yīng)的controller所以報(bào)404錯。
四. filter過濾器的配置
過濾器可以看做是AOP的一種實(shí)現(xiàn),可以對某一類的請求進(jìn)行截獲并添加一些額外的處理
<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看字面意思就知道,是一個編碼過濾器,它會截獲所有的請求,并將他們統(tǒng)一轉(zhuǎn)化成utf-8(通過我們設(shè)置的encoding參數(shù))
shiroFilter是shiro安全框架的過濾器,我們通過過濾所有請求,可以對不同請求附加不同的角色和權(quán)限要求
比較簡單的項(xiàng)目,web.xml配置就這些了,結(jié)束。
|