Sitemesh 是一个网页布局和修饰的框架,基于 Servlet 中的 Filter,类似于 ASP.NET 中的‘母版页’技术。
对于大部分网站的布局,基本都是分上下侧中布局。上下为头部尾部,侧栏通常为菜单栏,而中部多为主要内容块。
我们都知道,网站的布局是规律的,通常头尾侧部内容都是固定,变动少,而中部主要内容根据业务情况改变,所以采取装饰器不仅可以有效减少代码开发量,
也更加使得前端系统更附合代码的正交原则。
以上只是个人见解,如有错误,欢迎更正。
1.目前sitemesh版本比较常用的是sitemesh3。至于包请根据自身情况下载,maven配置方式的也可以上网找到相关pom配置。
2.web.xml加载sitemesh
<web-app>
...
<filter>
<filter-name>sitemesh</filter-name>
<filter-class>org.sitemesh.config.ConfigurableSiteMeshFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>sitemesh</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
3.子母页相关配置
内容页-"需被装饰的页面"
<!DOCTYPE html>
<html>
<head>
<title>内容页的标题</title>
</head>
<body>
内容页的body部分
</body>
</html>
母版页-"装饰页面"
<!DOCTYPE html>
<html>
<head>
<title>
<sitemesh:write property='title' /> - ltcms
</title>
<sitemesh:write property='head' />
</head>
<body>
<header>header</header>
<hr />
demo.html的title将被填充到这儿:
<sitemesh:write property='title' /><br />
demo.html的body将被填充到这儿:
<sitemesh:write property='body' />
<hr />
<footer>footer</footer>
</body>
</html>
4.配置装饰器
<?xml version="1.0" encoding="UTF-8"?>
<sitemesh>
<!-- 指明满足“/*”的页面,将被“/WEB-INF/views/decorators/decorator.html”所装饰 -->
<mapping path="/*" decorator="/WEB-INF/views/decorators/decorator.html" />
<!-- 指明满足“/exclude.jsp*”的页面,将被排除,不被装饰 -->
<mapping path="/exclude.jsp*" exclue="true" />
</sitemesh>
以上是sitemesh基本使用,如果需要更多的功能,可以自行上网搜索。