5 Spring 入門 web.xml配置詳解
1.在WEB-INF的lib下面匯入jar包
2.在web.xml裡面配置Spring Mvc ,即,配置DispatcherServlet。(DispatcherServlet實際上就是一個servlet。)
3. 啟動Spring
4.spring-servlet.xml配置【
spring-servlet.xml配置
spring-servlet這個名字是因為上面web.xml中<servlet-name>標籤配的值為spring(<servlet-name>spring</servlet-name>),再加上“-servlet”字尾而形成的spring-servlet.xml檔名,如果改為springMVC,對應的檔名則為springMVC-servlet.xml。
】
5.applicationContext檔案命名為:-context.xml,如 root-context.xml;(如果有上下文的話)
Spring分為多個檔案進行分別的配置,其中在servlet-name中如果沒有指定init-param屬性,那麼系統自動尋找的spring配置檔案為[servlet-name]-servlet.xml。
當需要載入多個spring相關的配置檔案時,首先載入ContextLoaderListener類,再指定context-param中指定多個spring配置檔案,使用逗號分別隔開各個檔案。為了使用方便可以將配置檔案進行MVC式的分解,配置控制器Bean的配置檔案放置在一個xml檔案中,server的Bean放在service.xml檔案中。
<servlet-mapping>指定的該servlet接管的url的行為,此處為了簡便起見使用*.*,則表示在URL只要是在本機使用的任何request都是由該dispatchServlet來處理。
目前,spring提供了兩種載入器,以供web容器的載入:一種是ContextLoaderListener,另一種是ContextLoaderServlet。這兩種在功能上完全相同,只是一種是基於Servlet2.3版本中新引入的Listener介面實現,而另一種是基於Servlet介面實現,以下是這兩種載入器在web.xml中的時機配置應用:
第一種:
<listener>
<listener-class>org.springframework.context.ContextLoaderListener</listener-class>
</listener>
另一種:
<servlet>
<servlet-name>context</servlet-name>
<servlet-class>org.springframework.context.ContextLoaderServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
通過上面的配置,web容器會自動載入applicationcontext.xml初始化。
如果需要指定配置檔案的位置,可通過context-param加以指定:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/myApplicationContext.xml</param-value>
</context-param>
之後,可以通過
WebApplicationContextUtils.getWebApplicationContext方法在web應用中獲取applicationcontext的引用。
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- x詳解
-----------------------------------------------------------------------------------------------------------------------------------
2.詳解DispatcherServlet處理的請求必須在同一個web.xml
檔案裡使用url-mapping定義對映。
<!-- 配置Spring MVC DispatcherServlet -->
A:配置<servet></servet>和<servet-mapping></servet-mapping>
B: <servlet>
<servlet-name>test</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:source/servlet/test-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>test</servlet-name>
<url-pattern>/*.do</url-pattern>
</servlet-mapping>
這樣所有以.do結尾的請求都會被servlet test處理。
在DispatcherServlet
的初始化過程中,Spring會在web應用的WEB-INF
資料夾下尋找名為[servlet-name]-servlet.xml
的配置檔案,生成檔案中定義的bean。這些bean會覆蓋在全域性範圍(global cope)中定義的同名的bean。如果servletName-servlet.xml不在預設路徑下必須顯示指定。【<param-value>classpath:source/servlet/test-servlet.xml</param-value>】
<!-- 載入spring的xml配置檔案到 spring的上下文容器中 -->
在需要多個Spring xml檔案的時候 <context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:root-context.xml</param-value>
</context-param>
3.詳解<!-- 監聽spring上下文容器 -->
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
存放位置:
1:src下面需要在web.xml中定義如下:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
2:WEB-INF下面需要在web.xml中定義如下:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/applicationContext*.xml</param-value>
</context-param>
web.xml通過contextConfigLocation配置spring的方式
SSI框架配置檔案路徑問題:
struts2的 1個+N個路徑:src+src(可配置) 名稱: struts.xml + N
spring 的 1個路徑:src 名稱: applicationContext.xml
ibatis 的 1個+N個路徑: src+src(可配置) 名稱: SqlMapConfig.xml + N
部署到tomcat後,src目錄下的配置檔案會和class檔案一樣,自動copy到應用的 classes目錄下
spring的配置檔案在啟動時,載入的是web-info目錄下的applicationContext.xml,
執行時使用的是web-info/classes目錄下的applicationContext.xml。
配置web.xml使這2個路徑一致:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/applicationContext.xml</param-value>
</context-param>
多個配置檔案的載入
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath*:conf/spring/applicationContext_core*.xml,
classpath*:conf/spring/applicationContext_dict*.xml,
classpath*:conf/spring/applicationContext_hibernate.xml,
classpath*:conf/spring/applicationContext_staff*.xml,
classpath*:conf/spring/applicationContext_security.xml
classpath*:conf/spring/applicationContext_modules*.xml
classpath*:conf/spring/applicationContext_cti*.xml
classpath*:conf/spring/applicationContext_apm*.xml
</param-value>
</context-param>
contextConfigLocation引數定義了要裝入的 Spring配置檔案。
首先與Spring相關的配置檔案必須要以"applicationContext-"開頭,要符合約定優於配置的思想,這樣在效率上和出錯率上都要好很多。還有最好把所有Spring配置檔案都放在一個統一的目錄下,如果專案大了還可以在該目錄下分模組建目錄。這樣程式看起來不會很亂。在web.xml中的配置如下:
Xml程式碼
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:**/applicationContext-*.xml</param-value>
</context-param>
"**/"表示的是任意目錄;
"**/applicationContext-*.xml"表示任意目錄下的以"applicationContext-"開頭的XML檔案。你自己可以根據需要修改。最好把所有Spring配置檔案都放在一個統一的目錄下,如:
<!-- Spring的配置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:/spring/applicationContext-*.xml</param-value>
</context-param>
web.xml中classpath:和classpath*:,有什麼區別?
classpath:只會到你的class路徑中查詢找檔案;
classpath*:不僅包含class路徑,還包括jar檔案中(class路徑)進行查詢.
相關推薦
5 Spring 入門 web.xml配置詳解
1.在WEB-INF的lib下面匯入jar包2.在web.xml裡面配置Spring Mvc ,即,配置DispatcherServlet。(DispatcherServlet實際上就是一個servlet。)3. 啟動Spring4.spring-servlet.xml配置【
Spring 入門 web.xml配置詳解
.net .html tle spring tail pri .com http https Spring 入門 web.xml配置詳解 https://www.cnblogs.com/cczz_11/p/4363314.html https://blog.csdn.ne
Spring MVC的web.xml配置詳解(轉)
出處http://blog.csdn.net/u0107967901、spring 框架解決字串編碼問題:過濾器 CharacterEncodingFilter(filter-name) 2、在web.xml配置監聽器ContextLoaderListener(listene
web.xml配置詳解
知識 其他 location 參數 pin systems doctype doc clu web.xml配置詳解 引文: 對於一個J2EE領域的程序員而言,基本上每天都會和web應用打交道。 什麽是web應用?最簡單的web應用什麽樣?給你一個web應用你該從何入手
Web.xml配置詳解之context-param
ltr 完成 數據庫 數據 鍵值對 art str htm 方法 轉自:http://blog.csdn.net/liaoxiaohua1981/article/details/6759206 格式定義: [html] view plaincopy
Java學習02-web.xml配置詳解
log 用戶授權 相對 lte 聯合 page int config 定制 <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSche
深入淺出javaEE系列(一)---web.xml配置詳解
web.xml是web專案最重要的一個檔案 一:定義頭和根元素 <?xml version="1.0" encoding="UTF-8"?> 部署描述符檔案就像所有xml檔案一樣,必須以一個xml頭開始。這個頭宣告必
Web.xml配置詳解之context-param,listener
格式定義: [html] view plain copy <c
Web.XML 配置詳解
每一個站的WEB-INF下都有一個web.xml的設定檔案,它提供了我們站臺的配置設定. web.xml定義: .站臺的名稱和說明 .針對環境引數(Context)做初始化工作 .Servlet的名稱和對映 .Session的設定 .Tag library的對映 .JSP網頁
java web工程web.xml配置詳解
Web.xml詳解: 1.web.xml載入過程(步驟) 首先簡單講一下,web.xml的載入過程。當啟動一個WEB專案時,容器包括(JBoss、Tomcat等)首先會讀取專案web.xml配置檔案裡的配置,當這一步驟沒有出錯並且完成之後,專案才能正常地被啟動起來。
javaweb:web.xml配置詳解
Web.xml詳解: 1.web.xml載入過程(步驟) 首先簡單講一下,web.xml的載入過程。當啟動一個WEB專案時,容器包括(JBoss、Tomcat等)首先會讀取專案web.xml配置檔案裡的配置,當這一步驟沒有出錯並且完成之後,專案才能正常地被啟動起來。 1
java web工程web xml配置詳解
<!--****************************過濾器配置*********************************--> <!-- 字符集過濾器 --> <filter> <filter-name>Characte
(轉)Spring boot——logback.xml 配置詳解(二)
回到頂部1 根節點<configuration>包含的屬性scan:當此屬性設定為true時,配置檔案如果發生改變,將會被重新載入,預設值為true。scanPeriod:設定監測配置檔案是否有修改的時間間隔,如果沒有給出時間單位,預設單位是毫秒。當scan為true時,此屬性生效。預設的時間間隔
servlet的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 boot——logback.xml 配置詳解(二)
原文地址:https://www.cnblogs.com/lixuwu/p/5810912.html https://aub.iteye.com/blog/1101260 閱
Java Web專案web.xml配置詳解與示例
<description>,<display-name>,<icon> <description>站臺描述</discription> 對站臺做出描述. <display-name>站臺名稱</display-name> 定
ssm框架中,專案啟動過程以及web.xml配置詳解
本篇主要在基於SSM的框架,深入講解web.xml的配置 web.xml 每個javaEE專案中都會有,web.xml檔案是用來初始化配置資訊:比如Welcome頁面、servlet、servlet-mapping、filter、listener、啟動載入級
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
spring mvc 配置(xml配置詳解)
如果您曾經使用Spring MVC框架開發過Web應用程式,本文提供關於Spring MVC框架的配置技巧,以幫助管理基於Spring的web應用程式的多個例項。 web.xml 配置: 1 2 3 4
setting.xml 配置詳解
校驗 找不到 順序 裁剪 全局 -- mls leg 觸發 文件存放位置 全局配置: ${M2_HOME}/conf/settings.xml 用戶配置: ${user.home}/.m2/settings.xml note:用戶配置優先於全局配置。${user.home}