使用 SpringSource Slices 進行可插拔樣式

工程 | Andy Wilkinson | 2009 年 7 月 10 日 | ...

自從我們 宣佈 SpringSource Slices 以來,許多使用者和客戶詢問有關使用 Slices 使其網站的樣式和品牌可插拔的問題。在本文中,我將演示使用 Slices 來實現這一目標的簡便性。

可插拔樣式

我有一個標準的 war 檔案,名為 styled.host.war,其中包含一個非常簡單的 index.html 頁面
<html>
	<head>
		<title>SpringSource Slices Pluggable Styling Demonstration</title>
		<link rel="StyleSheet" href="styles/main.css" type="text/css" />
	</head>
	<body>
		<div class="header">
			<div class="title">SpringSource Slices</div>
			<div class="subtitle">Pluggable Styling Demonstration</div>
		</div>
	</body>
</html>

正如您所見,它正在尋找一個名為 styles/main.css 的 CSS 檔案。如果沒有 Slice,這個檔案就不存在。將 war 部署到 dm Server 顯示頁面沒有樣式

cp styled.host.war ~/springsource-dm-server-2.0.0.CI-B297/pickup/
[2009-07-10 15:20:46.183] fs-watcher <SPDE0048I> Processing 'CREATED' event for file 'styled.host.war'.
[2009-07-10 15:20:46.525] fs-watcher <SPDE0010I> Deployment of 'styled.host' version '1.0.0' completed.
[2009-07-10 15:20:46.539] Thread-19  <SPWE0000I> Starting web bundle '/styling'.
[2009-07-10 15:20:46.965] Thread-19  <SPWE0001I> Started web bundle '/styling'.
Unstyled index page

透過部署一個指定樣式化宿主 war 作為其宿主並提供所需樣式表的 Slice,可以輕鬆地對頁面進行樣式化

cp plain.style.slice.war ~/springsource-dm-server-2.0.0.CI-B297/pickup/
[2009-07-10 15:28:30.699] fs-watcher <SPDE0048I> Processing 'CREATED' event for file 'plain.style.slice.war'.
[2009-07-10 15:28:30.789] fs-watcher <SPDE0010I> Deployment of 'plain.style.slice' version '1.0.0' completed.

現在如果我們檢視頁面,它的外觀已經改變,這要歸功於 Slice 提供的樣式

Index page with the plain style applied

請注意,無需重新部署或修改 Host war 檔案,當 Slice 部署時,Host 會立即採用新的樣式。同樣,如果我們現在取消部署樣式 Slice,Host 將恢復到我們之前看到的未樣式化的外觀。或者,我們可以更進一步,移除這個樣式 Slice 並部署另一個來更改樣式,同樣無需重新部署或修改 Host

rm ~/springsource-dm-server-2.0.0.CI-B297/pickup/plain.style.slice.war
cp green.style.slice.war ~/springsource-dm-server-2.0.0.CI-B297/pickup/
[2009-07-10 15:34:48.948] fs-watcher <SPDE0048I> Processing 'DELETED' event for file 'plain.style.slice.war'.
[2009-07-10 15:34:49.038] fs-watcher <SPDE0012I> Undeployment of 'plain.style.slice' version '1.0.0' completed.
[2009-07-10 15:36:01.064] fs-watcher <SPDE0048I> Processing 'CREATED' event for file 'green.style.slice.war'.
[2009-07-10 15:36:01.146] fs-watcher <SPDE0010I> Deployment of 'green.style.slice' version '1.0.0' completed.
Index page with the green style applied

它是如何工作的?

充分利用 Slice 的這種功能非常容易。

宿主

主宿主在其 web.xml 檔案中將 Slices 的 SliceHostFilter 對映到 /*
<web-app version="2.5" 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_2_5.xsd">
			
    <filter>
        <filter-name>host-filter</filter-name>
        <filter-class>com.springsource.osgi.slices.core.SliceHostFilter</filter-class>    	
    </filter>

    <filter-mapping>
        <filter-name>host-filter</filter-name>
        <url-pattern>/*</url-pattern>        
    </filter-mapping>
</web-app>

此 Filter 會將任何與宿主的任何 Slice 匹配的路徑的請求路由到匹配的 Slice。如果找不到匹配的 Slice,請求將直接傳遞給宿主。

正如我們在上面的 index.html 檔案中看到的,宿主正在 /styles 目錄中查詢其所有樣式,但實際上並不自己提供此內容。

樣式 Slices

每個樣式 Slice 也非常簡單。在其 MANIFEST.MF 檔案中,它們使用 Slice-Host 頭指定它們的宿主,並使用 Slice-ContextPath 頭將上下文路徑設定為 /styles
Manifest-Version: 1.0
Bundle-SymbolicName: green.style.slice
Bundle-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: Styling Slice
Slice-Host: styled.host;version="[1.0, 2.0)"
Slice-ContextPath: /styles

這裡的關鍵是配置的上下文路徑 /styles 與宿主查詢樣式的目錄匹配。這意味著,當 Slice 部署時,宿主中的過濾器會將對 /styles 的請求路由到 Slice。剩下的就是樣式 Slice 包含樣式化宿主所需的任何資源。在本例中,它是 main.css 檔案,宿主在其 index.html 中引用了它,以及一個從 CSS 引用的圖片

The content of the green styling slice

瞭解更多

上面使用的 war 檔案的原始碼可在 Slices Git 儲存庫 (git://git.springsource.org/slices/slices.git) 的 samples/pluggable-styling 目錄中找到。另外,請檢視 Rob 的 公告部落格,其中包含有關如何構建 Slices 並將其安裝到 dm Server 的詳細資訊。

獲取 Spring 新聞通訊

透過 Spring 新聞通訊保持聯絡

訂閱

領先一步

VMware 提供培訓和認證,助您加速進步。

瞭解更多

獲得支援

Tanzu Spring 提供 OpenJDK™、Spring 和 Apache Tomcat® 的支援和二進位制檔案,只需一份簡單的訂閱。

瞭解更多

即將舉行的活動

檢視 Spring 社群所有即將舉行的活動。

檢視所有