Sitemesh3的配置和使用,sitemesh也就是我们平时说的装饰器框架,其实作用和jsp的include方法差不多。
OS(OpenSymphony)的SiteMesh是一个用来在JSP中实现页面布局和装饰(layout and decoration)的框架组件。
能够帮助网站开发人员较容易实现页面中动态内容和静态装饰外观的分离。
sitemesh是通过servlet的fileter过滤器拦截response响应来实现的。 SpringMVC中也是同样的道理,通过SpringMVC封装的过滤器,来实现对也页面的过滤。 相同点:都能提高公共模块的复用性,提高开发效率。 不同点:include需要把需要用到的jsp文件写死到每一个jsp文件中,而sitemesh则是动态的-> 使用filter过滤response请求,然后把装饰完成的页面,返回给前端。区别还是很大的。 我们可以简单的理解:sitemesh是动态的,提供了更高的灵活性; 而传统的include方法是静态的,一旦需要改变公共部分,使用include是一件很麻烦的事情。
我自己的理解就是如果很多个jsp页面都引用了某个页面,如果这个页面突然改个名字什么的(以前叫xxx.jsp,现在叫xxxx.jsp),那include这个jsp的所有页面都需要去修改,虽然我觉得没这么zz去改这个名字,不过既然接触到了sitemesh3这个装饰器框架,就试试玩玩(看到网上也有人讨论sitemesh3的性能问题,这个我倒是还没仔细研究过)。
1.首先就是介绍和引入jar包的问题。
这个我用的是maven,搜出来几个找了个稍微靠谱点的。(Sitemesh 是一个网页布局和修饰的框架,基于 Servlet 中的 Filter,类似于 ASP.NET 中的‘母版页’技术。参考:百度百科,相关类似技术:Apache Tiles。如果深入研究请移步官网。https://wiki.sitemesh.org/wiki/display/sitemesh/Home )
<!-- https://mvnrepository.com/artifact/org.sitemesh/sitemesh --> <dependency> <groupId>org.sitemesh</groupId> <artifactId>sitemesh</artifactId> <version>3.0.1</version> </dependency>
2.配置sitemesh3的过滤器到web.xml中(这里博主是在使用spring security看到的sitemesh3,看到别人的配置中说了这么一句。sitemesh装饰器放在spring security过滤器的后面,以免装饰器页中的security不起作用)
<filter> <filter-name>sitemesh</filter-name> <filter-class>org.sitemesh.config.ConfigurableSiteMeshFilter</filter-class> <init-param> <param-name>ignore</param-name> <param-value>true</param-value> </init-param> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>sitemesh</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
3.准备一个测试页面test.html(jsp也可测试),和decorator.html
test.html是我们打开的网页,他将会被decorator.html装饰
<!DOCTYPE html> <html> <head> <title>内容页的标题</title> </head> <body> 内容页的body部分 </body> </html>
然后是decorator.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>
这一段其实就是用到sitemesh的标签库,把内容页面的title啊,body什么的填到这个装饰器页面。
4.接下来就是配置sitemesh3.xml文件,把他扔到/WEB-INF下面,跟web.xml同级。这里贴上我的简单配置吧。
<?xml version="1.0" encoding="UTF-8"?> <sitemesh> <!--这句的意思就是制定所有页面都经过装饰器decorator装饰 --> <mapping path="/*" decorator="/WEB-INF/views/decorators/decorator.html" /> <!-- 这句的意思是/druid/下的所有子页面都不经过装饰器页面装饰 --> <mapping path="/druid/*" exclude="true" /> <!-- 这句的意思跟上面那行差不多,但是这里有 exclude 和exclue的区别,其实两个都是可以的,不知道是不是开发的程序员打错了呢,还是博主英语太差 --> <mapping path="/manager/*" exclue="true"/> </sitemesh>
以上就是基本内容,你配置完成测试应该就能看到基本效果了。
5.sitemesh3.xml拓展配置详解
<sitemesh> <!--默认情况下, sitemesh 只对 HTTP 响应头中 Content-Type 为 text/html 的类型进行拦截和装饰, 我们可以添加更多的 mime 类型--> <mime-type>text/html</mime-type> <mime-type>application/vnd.wap.xhtml+xml</mime-type> <mime-type>application/xhtml+xml</mime-type> <!-- 默认装饰器,当下面的路径都不匹配时,启用该装饰器进行装饰 --> <mapping decorator="/default-decorator.html"/> <!-- 对不同的路径,启用不同的装饰器 --> <mapping path="/admin/*" decorator="/another-decorator.html"/> <mapping path="/*.special.jsp" decorator="/special-decorator.html"/> <!-- 对同一路径,启用多个装饰器 --> <mapping> <path>/articles/*</path> <decorator>/decorators/article.html</decorator> <decorator>/decorators/two-page-layout.html</decorator> <decorator>/decorators/common.html</decorator> </mapping> <!-- 排除,不进行装饰的路径 --> <mapping path="/javadoc/*" exclue="true"/> <mapping path="/brochures/*" exclue="true"/> <!-- 自定义 tag 规则 --> <content-processor> <tag-rule-bundle class="com.something.CssCompressingBundle" /> <tag-rule-bundle class="com.something.LinkRewritingBundle"/> </content-processor> </sitemesh>
6.自定义Tag规则
Sitemesh 3 默认只提供了 body,title,head 等 tag 类型,我们可以通过实现 TagRuleBundle 扩展自定义的 tag 规则:
public class MyTagRuleBundle implements TagRuleBundle { @Override public void install(State defaultState, ContentProperty contentProperty, SiteMeshContext siteMeshContext) { defaultState.addRule("myHeader", new ExportTagToContentRule(contentProperty.getChild("myHeader"), false)); } @Override public void cleanUp(State defaultState, ContentProperty contentProperty, SiteMeshContext siteMeshContext) { } }
最后在 sitemesh3.xml 中配置即可:
<content-processor> <tag-rule-bundle class="com.lt.common.ext.sitemesh3.MyTagRuleBundle" /> </content-processor>
关于最后的自定义规则,博主也没去试试,写下来以后用的时候方便,主要多看看sitemesh3.xml的拓展配置。