使用 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

透過部署一個 Slice 來指定樣式的主機 war 檔案作為其主機並提供所需的樣式表,可以輕鬆地設定頁面樣式

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 檔案,Host 只需在 Slice 部署時拾取新樣式即可。 同樣,如果我們現在取消部署樣式 Slice,Host 將恢復到我們之前看到的未樣式的外觀。 或者,我們可以更進一步,刪除此樣式 Slice 並部署另一個 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

它是如何工作的?

利用 Slices 的這一功能非常容易。

Host

主主機在它的 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>

此過濾器會將路徑與任何 Hosts 的 Slices 匹配的任何請求路由到匹配的 Slice。 如果找不到匹配的 Slice,請求只會傳遞到 Host。

正如我們上面在 index.html 檔案中看到的那樣,主機正在尋找 /styles 目錄中的所有樣式,但實際上並不提供這些內容本身。

樣式 Slices

每個樣式 Slices 也非常簡單。 在它們的 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 與 Host 正在查詢其樣式的位置相匹配。 這意味著,當部署 Slice 時,Host 中的過濾器會將傳送到 /styles 的請求路由到 Slice。 剩下的就是讓樣式 slice 包含設定 Host 樣式所需的任何資源。 在這種情況下,它是 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 社群中所有即將舉行的活動。

檢視全部