轉載:https://blog.csdn.net/qing_gee/article/details/50965562javascript
現代Web應用程序普遍使用MVC(model、view、controller)模式,那麼SpringMVC就剛好能夠輕鬆幫咱們搭建一個Web開發環境。而要搭好開發環境,熟知SpringMVC的三個XML(web.xml、application-context.xml、context-dispatcher.xml)就顯得必不可少。而我呢,雖然前先後後左左右右也大見過三次Web框架,但每次都糾結的要了老命,那麼痛定思痛,我決定下功夫把三個XML給搞得有條理些。css
不過呢,做爲一個軟件開發的全棧工程師(自黑不是自詡,身爲創業團隊的負責人,打雜工的角色我就只能勉爲其難),對於三個XML的看法只停留在認知的層面,整篇文章的敘述不免有不專業的地方,各位光臨的朋友儘管指摘,我將虛心接受。html
本篇先來介紹web.xml,瞭解其中必要的配置項,爲之後順利開發打下堅實基礎。java
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>ymeng</display-name> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:application-context.xml</param-value> </context-param> <!-- set character encoding spring --> <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> <filter> <filter-name>sessionValidate</filter-name> <filter-class> com.honzh.common.filter.SessionValidateFilter </filter-class> <init-param> <param-name>uri</param-name> <param-value>/deal/</param-value> </init-param> <init-param> <param-name>loginUrl</param-name> <param-value>/login</param-value> </init-param> <init-param> <param-name>backToUrl</param-name> <param-value>/</param-value> </init-param> <init-param> <param-name>debug</param-name> <param-value>true</param-value> </init-param> </filter> <!-- 把須要進行check登錄的請求放到此處 --> <filter-mapping> <filter-name>sessionValidate</filter-name> <url-pattern>/deal/*</url-pattern> </filter-mapping> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> <servlet> <servlet-name>web-app</servlet-name> <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:context-dispatcher.xml</param-value> </init-param> <!-- 使系統在啓動時裝在servlet而不是第一個servlet被訪問 --> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>web-app</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!-- 配置session過時時間120分鐘 --> <session-config> <session-timeout>120</session-timeout> </session-config> <error-page> <error-code>404</error-code> <location>/404.jsp</location> </error-page> </web-app>
<context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:application-context.xml</param-value> </context-param> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener>
關於字符集過濾器,就無需多言,從xml配置上就能夠看得出來,其做用就是爲了防止亂碼,固然最開始在接觸struts2的時候,被中文亂碼困擾的內心都有了挫敗感,但springMVC輕輕鬆鬆搞定了這個煩惱。web
配置的方式基本固定以下:spring
<!-- set character encoding spring --> <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字符集過濾器CharacterEncodingFiltermarkdown
sessionValidate的過濾器對於個人項目來講就很關鍵了,其主要做用就是爲了在頁面跳轉時檢查session有沒有失效(包含超時、未登錄),而後呢,若是驗證失敗,能夠跳轉到首頁登錄,登錄完成呢,又能夠回到原始的頁面(以下圖所示,點擊個人資源,系統發現我沒有登錄,那麼彈出登錄窗口,當我登錄完成後,顯示個人資源頁面)。session
<filter> <filter-name>sessionValidate</filter-name> <filter-class> com.honzh.common.filter.SessionValidateFilter </filter-class> <init-param> <param-name>uri</param-name> <param-value>/deal/</param-value> </init-param> <init-param> <param-name>loginUrl</param-name> <param-value>/login</param-value> </init-param> <init-param> <param-name>backToUrl</param-name> <param-value>/</param-value> </init-param> <init-param> <param-name>debug</param-name> <param-value>true</param-value> </init-param> </filter> <!-- 把須要進行check登錄的請求放到此處 --> <filter-mapping> <filter-name>sessionValidate</filter-name> <url-pattern>/deal/*</url-pattern> </filter-mapping>
這個配置我稍候會用一整篇的文章來介紹,因此這裏只作一個引導。app
DispatcherServlet是springMVC自帶的一個開箱即用的調度員,這個調度員就和context-dispatcher.xml聯繫起來了。框架
<servlet> <servlet-name>web-app</servlet-name> <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:context-dispatcher.xml</param-value> </init-param> <!-- 使系統在啓動時裝在servlet而不是第一個servlet被訪問 --> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>web-app</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>
- *.html 只能響應html格式的請求。
- /*模式表示沒有映射特定類型的響應,這會在處理圖片或者樣式css時帶來沒必要要的麻煩。
<!-- 配置session過時時間120分鐘 --> <session-config> <session-timeout>120</session-timeout> </session-config>
session-timeout就不作詳細說明了,很直白,多說無益。有興趣的話能夠繼續瞭解Java Web開發Session超時設置。
<error-page> <error-code>404</error-code> <location>/404.jsp</location> </error-page>
經過該配置,當服務端出現404錯誤時就會跳轉到404.jsp頁面。
那麼首先,咱們來看一下普通的404頁面寫法。
<%@ page contentType="text/html;charset=UTF-8" pageEncoding="UTF-8"%><%@ include file="/components/common/taglib.jsp"%> <!DOCTYPE HTML> <html> <head> <meta http-equiv='Refresh' content='3;URL=${ctx}/'> <title>404 錯誤</title> </head> <body> <p>此頁面正在開發中...</p> <p>系統將在 <span style="color:red;">3</span> 秒後跳轉到首頁,或者直接點擊 <a href="javascript:history.back()">返回</a></p> </body> </html>
當出現404時,首先提示用戶頁面訪問不到,而後手動跳轉到首頁或者3秒後跳轉到首頁。