Spring MVC +Mybatis + Maven 配置之web.xml配置
阿新 • • 發佈:2019-02-08
web.xml作用,以及web容器執行順序
每個網站一般都有一個web.xml,被用來初始化配置資訊,但是又不是必須的。web.xml是web容器首先讀取的檔案。web容器執行的順序是:1、啟動一個WEB專案的時候,WEB容器會去讀取它的配置檔案web.xml,讀取<listener>和<context-param>兩個結點。
2、容器建立一個ServletContext(servlet上下文),這個web專案的所有部分都將共享這個上下文。
3、容器將<context-param>轉換為鍵值對,並交給servletContext。
4、容器建立<listener>中的類例項,建立監聽器。
web.xml中配置執行順序ServletContext-> context-param ->listener -> filter -> servlet
web.xml中配置的專案內容
- 定義log4j配置檔案的載入
- 定義應用程式的初始化
- 定義Spring MVC 配置
- 定義防止spring記憶體溢位配置
- 定義請求的字符集過濾器
- 定義整個應用程式訪問的預設頁
- 定義jstl自定義函式檔案配置
- 定義驗證碼載入配置
- 定義載入dwr配置
- 定義session有效期限配置
- 定義自定義預設404、500頁面
- 定義應用程式是否支援分散式部署配置
- 定義mybatis配置
- 定義上下文監聽器配置
完整的web.xml配置內容
<context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:/spring/spring-content.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <listener> <listener-class> org.springframework.web.context.request.RequestContextListener </listener-class> </listener> <!--log4j配置檔案載入--> <context-param> <param-name>log4jConfigLocation</param-name> <param-value>classpath:setup/log4j.xml</param-value> </context-param> <!--啟動Log4jConfigListener監聽器 每60s監聽一次log4j.xml配置檔案的變化--> <context-param> <param-name>log4jRefreshInterval</param-name> <param-value>60000</param-value> </context-param> <listener> <listener-class> org.springframework.web.util.Log4jConfigListener </listener-class> </listener> <!--log4j.properites載入Log4j --> <context-param> <param-name>log4jConfigLocation</param-name> <param-value>classpath:log/console.properties</param-value> </context-param> <context-param> <param-name>log4jRefreshInterval</param-name> <!--Spring預設重新整理Log4j配置檔案的間隔,單位為millisecond --> <param-value>60000</param-value> </context-param> <listener> <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class> </listener> <!-- logback配置 --> <context-param> <param-name>logbackConfigLocation</param-name> <param-value>classpath:logback.xml</param-value> </context-param> <listener> <listener-class>ch.qos.logback.ext.spring.web.LogbackConfigListener</listener-class> </listener> <!--啟動應用程式初始化監聽器 ApplicationInitListener--> <listener> <listener-class>com.rrtong.frame.ApplicationInitListener</listener-class> </listener> <!-- Spring MVC 相關配置 --> <servlet> <servlet-name>Dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:setup/applicationContext-mvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <!-- 防止spring記憶體溢位監聽器 --> <listener> <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class> </listener> <servlet-mapping> <servlet-name>Dispatcher</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <!-- 定義過濾器 --> <filter> <filter-name>Set Character Encoding</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> </filter> <filter-mapping> <filter-name>Set Character Encoding</filter-name> <url-pattern>*.do</url-pattern> </filter-mapping> <!-- 定義預設頁面 --> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <!-- 自定義jstl標籤函式 --> <jsp-config> <taglib> <taglib-uri>http://tags.rrtong.com/pj</taglib-uri> <taglib-location>/WEB-INF/jstl/custom.tld</taglib-location> </taglib> <taglib> <taglib-uri>http://java.sun.com/jsp/jstl/rrtong</taglib-uri> <taglib-location>/WEB-INF/jstl/unescape.tld</taglib-location> </taglib> </jsp-config> <!--定義驗證碼--> <servlet> <servlet-name>Kaptcha</servlet-name> <servlet-class>com.google.code.kaptcha.servlet.KaptchaServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>Kaptcha</servlet-name> <url-pattern>/kaptcha.jpg</url-pattern> </servlet-mapping> <!-- 載入dwr --> <servlet> <servlet-name>dwr-invoker</servlet-name> <servlet-class>org.directwebremoting.spring.DwrSpringServlet</servlet-class> <init-param> <!-- dwr測試頁面,上線時設定為false --> <param-name>debug</param-name> <param-value>true</param-value> </init-param> <!-- dwr攔截 --> <init-param> <param-name>org.directwebremoting.extend.Remoter</param-name> <param-value>com.pinhuba.web.filter.dwrRemoter.DWRRemoter</param-value> </init-param> <init-param> <!-- 防止其他域提交訪問 --> <param-name>crossDomainSessionSecurity</param-name> <param-value>false</param-value> </init-param> <!-- 壓縮js --> <init-param> <param-name>scriptCompressed</param-name> <param-value>true</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <!-- 定義session過期時間 --> <session-config> <session-timeout>30</session-timeout> </session-config> <!-- 配置自定義錯誤頁面 --> <error-page> <error-code>404</error-code> <location>/error/404.html</location> </error-page> <error-page> <error-code>500</error-code> <location>/error/500.html</location> </error-page> <!--tomcat叢集配置--> <display-name>rrtong</display-name> <distributable/>
如果通過java讀取param-value,getServletContext().getInitParameter("my_param")方式可以讀取到資料。
<context-param>
<param-name>my_param</param-name>
<param-value>hello</param-value>
</context-param>