领先一步
VMware 提供培训和认证,以加速您的进步。
了解更多自从我们宣布 SpringSource Slices 以来,许多用户和客户询问了使用 Slices 来使他们的网站的样式和品牌可插拔的问题。 在这篇博客中,我将演示使用 Slices 是多么容易。
<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'.
通过部署一个 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 提供的样式,它的外观已经改变
请注意,无需重新部署或更改 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.
<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 目录中的所有样式,但实际上并不提供这些内容本身。
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 引用的图像
我在上面使用的 war 文件的源代码可在 Slices Git 存储库 (git://git.springsource.org/slices/slices.git) 的 samples/pluggable-styling 目录中找到。 另外,请查看 Rob 的 公告博客,其中包含有关如何构建 Slices 并将其安装到 dm Server 中的详细信息。