初學者很實用:Web.xml常用元素

java web開發中最基本的配置本件:Web.xml,很多新手看不大懂,沒關係,一起來看看常用的配置有哪些?
首先我們來看一個最精簡的Struts2+Spring+hibernate集合框架的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">  
  <display-name>SSHDemo-<span style="line-height: 21.6000003814697px; font-family: Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;">挨踢界小人物</span><span style="line-height: 1.8em; font-family: Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;"></display-name>   </span>
   
  <!-- 添加對spring的支持,也就是配置監聽器 -->  
  <context-param>  
    <param-name>contextConfigLocation</param-name>  
    <param-value>classpath:applicationContext.xml</param-value>  
  </context-param>  
    
    <listener>  
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
    </listener>  
      
  <!-- 添加對struts2的支持,也就是配置struts2 攔截器,這個攔截器會攔截請求 -->  
  <filter>  
    <filter-name>struts2</filter-name>  
    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>  
  </filter>   
  <!-- 當hibernate+spring配合使用的時候,如果設置了lazy=true,那麼在讀取數據的時候,當讀取了父數據後,  
     hibernate會自動關閉session,這樣,當要使用子數據的時候,系統會拋出lazyinit的錯誤,  
      這時就需要使用spring提供的 OpenSessionInViewFilter,OpenSessionInViewFilter主要是保持Session狀態  
      知道request將全部頁面發送到客戶端,這樣就可以解決延遲加載帶來的問題 -->  
   <filter>  
    <filter-name>openSessionInViewFilter</filter-name>  
    <filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>  
    <init-param>  
      <param-name>singleSession</param-name>  
      <param-value>true</param-value>  
    </init-param>  
  </filter>  
    <!-- struts2攔截匹配符號,/*表示匹配所有,當然你可以寫成*.action 或者*.do-->
  <filter-mapping>  
    <filter-name>struts2</filter-name>  
    <url-pattern>/*</url-pattern>  
  </filter-mapping>  
   <filter-mapping>  
    <filter-name>openSessionInViewFilter</filter-name>  
    <url-pattern>*.do,*.action</url-pattern>  
  </filter-mapping>  
    <!-- 這裏最簡單,歡迎頁,也就是訪問這個web項目彈出的以一個頁面 -->
  <welcome-file-list>  
    <welcome-file>index.jsp</welcome-file>  
  </welcome-file-list>  
</web-app>  

---------------------------------------------------下面給出常用標籤的介紹:---------------------------------------------------
<web-app>    
<display-name></display-name>定義了WEB應用的名字    
<description></description> 聲明WEB應用的描述信息    
 
<context-param></context-param> context-param元素聲明應用範圍內的初始化參數。    
<filter></filter> 過濾器元素將一個名字與一個實現javax.servlet.Filter接口的類相關聯(例如struts2的攔截器:org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter)。    
<filter-mapping></filter-mapping> 一旦命名了一個過濾器,就要利用filter-mapping元素把它與一個或多個servlet或JSP頁面相關聯。    
<listener></listener>servlet API的版本2.3增加了對事件監聽程序的支持,事件監聽程序在建立、修改和刪除會話或servlet環境時得到通知。    
                     Listener元素指出事件監聽程序類。(常用到的例如Spring監聽器:org.springframework.web.context.ContextLoaderListener)    
<servlet></servlet> 在向servlet或JSP頁面制定初始化參數或定製URL時,必須首先命名servlet或JSP頁面。Servlet元素就是用來完成此項任務的。    
<servlet-mapping></servlet-mapping> 服務器一般爲servlet提供一個缺省的URL:http://host/webAppPrefix/servlet/ServletName。    
            <span style="color: #008000;">  但是,常常會更改這個URL,以便servlet可以訪問初始化參數或更容易地處理相對URL。</span>在更改缺省URL時,使用servlet-mapping元素。    
 
<session-config></session-config> 如果某個會話在一定時間內未被訪問,服務器可以拋棄它以節省內存。    
          可通過使用HttpSession的setMaxInactiveInterval方法明確設置單個會話對象的超時值,或者可利用session-config元素制定缺省超時值。    
 
<mime-mapping></mime-mapping>如果Web應用具有想到特殊的文件,希望能保證給他們分配特定的MIME類型,則mime-mapping元素提供這種保證。    
<welcome-file-list></welcome-file-list> 指示服務器在收到引用一個目錄名而不是文件名的URL時,使用哪個文件。    
<error-page></error-page> 在返回特定HTTP狀態代碼時,或者特定類型的異常被拋出時,能夠制定將要顯示的頁面。    
<taglib></taglib> 對標記庫描述符文件(Tag Libraryu Descriptor file)指定別名。此功能使你能夠更改TLD文件的位置,    
                  而不用編輯使用這些文件的JSP頁面。    
<resource-env-ref></resource-env-ref>聲明與資源相關的一個管理對象。    
<resource-ref></resource-ref> 聲明一個資源工廠使用的外部資源。    
<security-constraint></security-constraint> 制定應該保護的URL。它與login-config元素聯合使用    
<login-config></login-config> 指定服務器應該怎樣給試圖訪問受保護頁面的用戶授權。它與sercurity-constraint元素聯合使用。    
<security-role></security-role>給出安全角色的一個列表,這些角色將出現在servlet元素內的security-role-ref元素    
                   的role-name子元素中。分別地聲明角色可使高級IDE處理安全信息更爲容易。    
<env-entry></env-entry>聲明Web應用的環境項。    
<ejb-ref></ejb-ref>聲明一個EJB的主目錄的引用。    
< ejb-local-ref></ ejb-local-ref>聲明一個EJB的本地主目錄的應用。    
</web-app>    
---------------------------------------------------想看更多Web.xml介紹?---------------------------------------------------

這是給初學者看的,基本上是夠用了。當然,Web.xml還有很多的配置,查看更多詳細配置。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章