web-fragment模組化使用

來源:互聯網
上載者:User

標籤:

用eclipse右鍵new->other->web->web fragment project

確定後修改dynamic web project name為你要輸出到的項目,當然可以手動將項目打包成jar檔案添加到另一個web項目的lib下,也是一樣的。

 

web fragment project:

 

 

web-fragment.xml

 

<?xml version="1.0" encoding="UTF-8"?><web-fragment id="WebFragment_ID" version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-fragment_3_1.xsd"><display-name>myFragmentTest</display-name>  <name>myFragmentTest</name>      <listener>         <listener-class>com.listener.RequestListener</listener-class>     </listener>     

    <!-- 設定web伺服器對這個xml檔案的讀取順序,一般在有多個web-fragment的時候會進行配置 -->

    <ordering>
      <before><others/></before>
    </ordering>

        </web-fragment>

 

requestListener.java

這是一個請求監聽器,每當你訪問一個頁面時就會觸發

package com.listener;import javax.servlet.ServletContext;import javax.servlet.ServletRequestEvent;import javax.servlet.ServletRequestListener;public class RequestListener implements ServletRequestListener {    @Override    public void requestDestroyed(ServletRequestEvent event) {        System.out.println("請求結束!");    }    @Override    public void requestInitialized(ServletRequestEvent event) {        ServletContext application = event.getServletContext();        Integer count = (Integer) application.getAttribute("count");        if(null == count) {            count = 0;            application.setAttribute("count", count + 1);        } else {            application.setAttribute("count", count + 1);        }                System.out.println("請求的次數為:" + (count + 1));    }}

 

這就是另一個web項目

 

打包成jar的requestListener被加到了lib下

當web容器啟動的時候會執行個體化這個jar包裡定義的listener

當你請求一個頁面的時候就會調用listener裡的方法。

web-fragment模組化使用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.