日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

Spring項(xiàng)目實(shí)踐(二)

 XSMforever 2019-05-21

上一篇講了spring項(xiàng)目中的pom.xml文件,pom文件主要作用是引入依賴(lài)庫(kù),設(shè)置編譯參數(shù)等
這一篇來(lái)講一下web.xml文件。
web.xml的學(xué)名叫做部署描述文件(DD),它不是Spring所特有的,而是在Servlet規(guī)范中定義的,是web應(yīng)用的配置文件。
我們還是按照之前的套路,一部分一部分的講解。

一.根標(biāo)簽
根標(biāo)簽是<web-app>,這塊比較簡(jiǎn)單,模板里也自帶,不再贅述

二.上下文配置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)簽并不是專(zhuān)門(mén)為spring所設(shè)計(jì)的。在servlet中,context-param標(biāo)簽用來(lái)配置上下文參數(shù),這個(gè)參數(shù)在整個(gè)web應(yīng)用中都是可以訪問(wèn)的。
同樣,listener也是servlet的概念。這個(gè)listener主要用來(lái)監(jiān)視servlet的生命周期。

而在上面的例子中,contextConfigLocation這個(gè)上下文參數(shù)用來(lái)指定spring的配置文件,ContextLoaderListener則用來(lái)監(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)鍵注釋?zhuān)?/p>

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ù)來(lái)配置。

三.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>

這里可以看到,說(shuō)是springmvc的配置,其實(shí)就是一個(gè)servlet的配置,不過(guò)servlet的名字叫springmvc
其實(shí)這個(gè)servlet的名字叫什么無(wú)所謂,你可以叫servlet123,關(guān)鍵是servlet-class。
servlet-class的值是DispatcherServlet,它是springmvc的一個(gè)前端控制器,它負(fù)責(zé):
1、文件上傳解析,如果請(qǐng)求類(lèi)型是multipart將通過(guò)MultipartResolver進(jìn)行文件上傳解析;
2、通過(guò)HandlerMapping,將請(qǐng)求映射到處理器(返回一個(gè)HandlerExecutionChain,它包括一個(gè)處理器、多個(gè)HandlerInterceptor攔截器);
3、通過(guò)HandlerAdapter支持多種類(lèi)型的處理器(HandlerExecutionChain中的處理器);
4、通過(guò)ViewResolver解析邏輯視圖名到具體視圖實(shí)現(xiàn);
5、本地化解析;
6、渲染具體的視圖等;
7、如果執(zhí)行過(guò)程中遇到異常將交給HandlerExceptionResolver來(lái)解析。

另外說(shuō)一下幾個(gè)參數(shù)的意思:
contextConfigLocation這個(gè)參數(shù)可以不配置,如果不配置的話,那么默認(rèn)的value就是/WEB-INF/[servlet名字]-servlet.xml,也就是說(shuō)上面的不配置跟配置沒(méi)區(qū)別。
load-on-startup表示啟動(dòng)容器時(shí)初始化該Servlet,這個(gè)參數(shù)沒(méi)有默認(rèn)值,如果不配置,或者當(dāng)這個(gè)值小于0的時(shí)候,表示請(qǐng)求該servlet時(shí)才會(huì)初始化。另外當(dāng)它大于等于0時(shí),值越小,該servlet的優(yōu)先級(jí)越高,應(yīng)用啟動(dòng)時(shí)就越先加載。
servlet-mapping是servlet跟url之間的一個(gè)映射關(guān)系。比如上面的例子,實(shí)際上創(chuàng)建了一個(gè)springmvc這個(gè)servlet跟url為:yourWebLocation/web/的一個(gè)映射。比如你的域名是myweb.com,那么url-pattern
這里的絕對(duì)路徑其實(shí)是myweb.com/web/。
這個(gè)映射有什么意義呢?它表示servlet會(huì)處理所有符合url規(guī)則的請(qǐng)求。在我們的例子里,表示springmvc這個(gè)servlet會(huì)處理所有來(lái)自你的網(wǎng)站的請(qǐng)求(因?yàn)橛成涞搅烁夸洠?/p>

PS:這里有個(gè)小知識(shí)點(diǎn),url-pattern的值,/跟/*有什么區(qū)別呢?
/和/*的區(qū)別:
< url-pattern > / < / url-pattern > 不會(huì)匹配到*.jsp,即:*.jsp不會(huì)進(jìn)入spring的 DispatcherServlet類(lèi) 。
< url-pattern > /* < / url-pattern > 會(huì)匹配*.jsp,會(huì)出現(xiàn)返回jsp視圖時(shí)再次進(jìn)入spring的DispatcherServlet 類(lèi),導(dǎo)致找不到對(duì)應(yīng)的controller所以報(bào)404錯(cuò)。

四. filter過(guò)濾器的配置
過(guò)濾器可以看做是AOP的一種實(shí)現(xiàn),可以對(duì)某一類(lèi)的請(qǐng)求進(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>

上面是我們比較常用的幾個(gè)過(guò)濾器。
encodingFilter看字面意思就知道,是一個(gè)編碼過(guò)濾器,它會(huì)截獲所有的請(qǐng)求,并將他們統(tǒng)一轉(zhuǎn)化成utf-8(通過(guò)我們?cè)O(shè)置的encoding參數(shù))
shiroFilter是shiro安全框架的過(guò)濾器,我們通過(guò)過(guò)濾所有請(qǐng)求,可以對(duì)不同請(qǐng)求附加不同的角色和權(quán)限要求

比較簡(jiǎn)單的項(xiàng)目,web.xml配置就這些了,結(jié)束。

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買(mǎi)等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類(lèi)似文章 更多