1. 程式人生 > >Spring MVC 配置檔案 web.xml檔案詳解

Spring MVC 配置檔案 web.xml檔案詳解

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">

    <!-- 在Spring框架中是如何解決從頁面傳來的字串的編碼問題的呢?
    下面我們來看看Spring框架給我們提供過濾器CharacterEncodingFilter
     這個過濾器就是針對於每次瀏覽器請求進行過濾的,然後再其之上添加了父類沒有的功能即處理字元編碼。
      其中encoding用來設定編碼格式,forceEncoding用來設定是否理會 request.getCharacterEncoding()方法,設定為true則強制覆蓋之前的編碼格式。-->
    <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>
        <init-param>
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>characterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <!-- 專案中使用Spring 時,applicationContext.xml配置檔案中並沒有BeanFactory,要想在業務層中的class 檔案中直接引用Spring容器管理的bean可通過以下方式-->
    <!--1、在web.xml配置監聽器ContextLoaderListener-->
    <!--ContextLoaderListener的作用就是啟動Web容器時,自動裝配ApplicationContext的配置資訊。因為它實現了ServletContextListener這個介面,在web.xml配置這個監聽器,啟動容器時,就會預設執行它實現的方法。
    在ContextLoaderListener中關聯了ContextLoader這個類,所以整個載入配置過程由ContextLoader來完成。
    它的API說明
    第一段說明ContextLoader可以由 ContextLoaderListener和ContextLoaderServlet生成。
    如果檢視ContextLoaderServlet的API,可以看到它也關聯了ContextLoader這個類而且它實現了HttpServlet    這個介面
    第二段,ContextLoader建立的是 XmlWebApplicationContext這樣一個類,它實現的介面是WebApplicationContext->ConfigurableWebApplicationContext->ApplicationContext->
    BeanFactory這樣一來spring中的所有bean都由這個類來建立
     IUploaddatafileManager uploadmanager = (IUploaddatafileManager)
     ContextLoaderListener.getCurrentWebApplicationContext().getBean("uploadManager");-->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <!--2、部署applicationContext的xml檔案-->
    <!--如果在web.xml中不寫任何引數配置資訊,預設的路徑是"/WEB-INF/applicationContext.xml,
    在WEB-INF目錄下建立的xml檔案的名稱必須是applicationContext.xml。
    如果是要自定義檔名可以在web.xml里加入contextConfigLocation這個context引數:
    在<param-value> </param-value>裡指定相應的xml檔名,如果有多個xml檔案,可以寫在一起並以“,”號分隔。
    也可以這樣applicationContext-*.xml採用萬用字元,比如這那個目錄下有applicationContext-ibatis-base.xml,
    applicationContext-action.xml,applicationContext-ibatis-dao.xml等檔案,都會一同被載入。
    在ContextLoaderListener中關聯了ContextLoader這個類,所以整個載入配置過程由ContextLoader來完成。-->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring/applicationContext.xml</param-value>
    </context-param>

    <!--如果你的DispatcherServlet攔截"/",為了實現REST風格,攔截了所有的請求,那麼同時對*.js,*.jpg等靜態檔案的訪問也就被攔截了。-->
    <!--方案一:啟用Tomcat的defaultServlet來處理靜態檔案-->
    <!--要寫在DispatcherServlet的前面, 讓 defaultServlet先攔截請求,這樣請求就不會進入Spring了,我想效能是最好的吧。-->
    <servlet-mapping>
        <servlet-name>default</servlet-name>
        <url-pattern>*.css</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>default</servlet-name>
        <url-pattern>*.swf</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>default</servlet-name>
        <url-pattern>*.gif</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>default</servlet-name>
        <url-pattern>*.jpg</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>default</servlet-name>
        <url-pattern>*.png</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>default</servlet-name>
        <url-pattern>*.js</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>default</servlet-name>
        <url-pattern>*.html</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>default</servlet-name>
        <url-pattern>*.xml</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>default</servlet-name>
        <url-pattern>*.json</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>default</servlet-name>
        <url-pattern>*.map</url-pattern>
    </servlet-mapping>
    <!--使用Spring MVC,配置DispatcherServlet是第一步。DispatcherServlet是一個Servlet,,所以可以配置多個DispatcherServlet-->
    <!--DispatcherServlet是前置控制器,配置在web.xml檔案中的。攔截匹配的請求,Servlet攔截匹配規則要自已定義,把攔截下來的請求,依據某某規則分發到目標Controller(我們寫的Action)來處理。-->
    <servlet>
        <servlet-name>DispatcherServlet</servlet-name><!--在DispatcherServlet的初始化過程中,框架會在web應用的 WEB-INF資料夾下尋找名為[servlet-name]-servlet.xml 的配置檔案,生成檔案中定義的bean。-->
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!--指明瞭配置檔案的檔名,不使用預設配置檔名,而使用dispatcher-servlet.xml配置檔案。-->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <!--其中<param-value>**.xml</param-value> 這裡可以使用多種寫法-->
            <!--1、不寫,使用預設值:/WEB-INF/<servlet-name>-servlet.xml-->
            <!--2、<param-value>/WEB-INF/classes/dispatcher-servlet.xml</param-value>-->
            <!--3、<param-value>classpath*:dispatcher-servlet.xml</param-value>-->
            <!--4、多個值用逗號分隔-->
            <param-value>classpath:spring/dispatcher-servlet.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup><!--是啟動順序,讓這個Servlet隨Servletp容器一起啟動。-->
    </servlet>
    <servlet-mapping>
        <!--這個Servlet的名字是dispatcher,可以有多個DispatcherServlet,是通過名字來區分的。每一個DispatcherServlet有自己的WebApplicationContext上下文物件。同時儲存的ServletContext中和Request物件中.-->
        <!--ApplicationContext是Spring的核心,Context我們通常解釋為上下文環境,我想用“容器”來表述它更容易理解一些,ApplicationContext則是“應用的容器”了:P,Spring把Bean放在這個容器中,在需要的時候,用getBean方法取出-->
        <servlet-name>DispatcherServlet</servlet-name>
        <!--Servlet攔截匹配規則可以自已定義,當對映為@RequestMapping("/user/add")時,為例,攔截哪種URL合適?-->
        <!--1、攔截*.do、*.htm, 例如:/user/add.do,這是最傳統的方式,最簡單也最實用。不會導致靜態檔案(jpg,js,css)被攔截。-->
        <!--2、攔截/,例如:/user/add,可以實現現在很流行的REST風格。很多網際網路型別的應用很喜歡這種風格的URL。弊端:會導致靜態檔案(jpg,js,css)被攔截後不能正常顯示。 -->
        <url-pattern>/</url-pattern> <!--會攔截URL中帶“/”的請求。-->
    </servlet-mapping>

    <welcome-file-list><!--指定歡迎頁面-->
        <welcome-file>login.html</welcome-file>
    </welcome-file-list>
    <error-page> <!--當系統出現404錯誤,跳轉到頁面nopage.html-->
        <error-code>404</error-code>
        <location>/nopage.html</location>
    </error-page>
    <error-page> <!--當系統出現java.lang.NullPointerException,跳轉到頁面error.html-->
        <exception-type>java.lang.NullPointerException</exception-type>
        <location>/error.html</location>
    </error-page>
    <session-config><!--會話超時配置,單位分鐘-->
        <session-timeout>360</session-timeout>
    </session-config>
</web-app>

相關推薦

Spring MVC 配置檔案 web.xml檔案

<?xml version="1.0" encoding="UTF-8"?> <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http

Struts應用配置web.xml檔案

#       執行機制:當伺服器啟動時還是會載入 B中<welcome-file>welcome.jsp</welcome-file>中的welcome.jsp,然後讀到(A)welcome.jsp中的 <logic:forwardname="welcome"/>,接下來

關於spring mvc 迴圈載入mapper.xml 檔案

一個spring mvc 的專案,啟動時發現一直 迴圈載入mapper.xml 檔案, 後來發現mapper.xml 檔案中,resultMap 的type 屬性設定的類不對,所以一直報錯,導致spring ,一直迴圈加截 mapper.xml檔案

實戰 :Spring MVC + 註解 +SqlServer 框架搭建及

原始碼下載:http://download.csdn.NET/detail/u010469432/6786687 https://blog.csdn.net/u010469432/article/details/17587699 先說一下Spring3 MVC的優點: spring&nb

web.xml標籤

<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/

實戰 :Spring MVC + 註解 +SqlServer 框架搭建及

先說一下Spring3 MVC的優點: spring MVC 屬於輕量級框架 1、Spring3 MVC的學習難度小於Struts2,Struts2用不上的多餘功能太多。呵呵,當然這不是決定因素。 2、Spring3 MVC很容易就可以寫出效能優秀的程式,Str

Spring MVC中常用註解之RequestMapping

SpringMVC和Struts2最大區別可能就是註解的使用。因為SpringMVC中可以實現在類這個層面上面配置資訊,也可以在方法層面上面配置資訊,既靈活又方便,不需要寫大量的配置檔案,也不需要寫大

Spring MVCweb.xml檔案配置

原文地址:http://blog.csdn.net/u010796790/article/details/52098258 1、spring 框架解決字串編碼問題:過濾器 CharacterEncodingFilter(filter-name)  2、在web.xml配置監

Spring MVC 配置檔案dispatcher-servlet.xml 檔案

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframew

spring mvc 配置web.xml servlet.xml檔案配置以及出現異常的解決方案

 java.io.FileNotFoundException: class path resource [beans.xml] cannot be opened because it does not  exist Could not open ServletContext

spring整合hibernate中的配置檔案hibernate.cfg.xml總結

applicationContext.xml配置檔案 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" x

通過web.xml檔案自動啟動spring容器載入對應的配置檔案

在web開發中可以在web.xml檔案中配置自啟動的servlet或者web容器監聽器,藉助兩者中的任何一者都可以完成啟動spring web應用上下文的工作。 下面給出使用web監聽器的方式來實現啟動是spring容器的配置檔案 <span style="font

spring Boot 填坑手冊: 無web.xml檔案時的上下文監聽的配置

    Spring Boot提倡Spring 4.x以上版本 使用基於註解的配置代替xml檔案配置 , 首當其衝的 , 便是 web.xml 配置全部消失了 。 那麼 ,當筆者想像其中注入監聽器和上下文時,遇到了難題 – 如何注入 ? 在哪裡注入 ?

web.xml檔案配置(servlet, spring, filter, listenr)的載入順序 研究

發現引起bug的原因是web.xml的下面幾行:    <filter-mapping>        <filter-name>SecurityFilter</filter-name>        <url-pattern>*.do</url-patt

spring-mvc-mybatis web.xml檔案

<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3

Spring ApplicationContext.xml 配置檔案常用註解和

ApplicationContext.xml <?xml version="1.0" encoding="UTF-8" ?> <beans xmlns="http://www.springframework.org/schema/beans"

Spring MVC配置檔案配置檢視解析器

spring mvc配置檔案 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="ht

spring配置檔案ApplicationContext.xml檔案裡面程式碼沒有提示功能

  由於編者水平有限,文中難免會有錯誤和疏漏,請各位讀者能提出寶貴建議或給予指正,可在博文下評論指出,我會及時改進,在此先感謝各位。   本文是自己學習所做筆記,歡迎轉載,但請註明出處:http://blog.csdn.NET/yijuanxia. 系統環境:Win7-6

classpath:和classpath*:的區別以及web.xml配置多個xml檔案

首先我們都知道要使用spring,則需要在web.xml中增加如下程式碼:  Xml程式碼    <listener>  <listener-class>org.springframework.web.context.ContextLoaderList

javaWeb中的web.xml檔案配置

在java工程中,web.xml用來初始化工程配置資訊,比如說welcome頁面,filter,listener,servlet,servlet-mapping,啟動載入級別等等。 每一個xml檔案都有定義他書寫規範的schema檔案,web.xml所對應的xml Sche