框架原理:类似于拦截器机制,不影响Servlet引擎接收和处理页面请求的逻辑,但是会在浏览器接收到返回内容前进行拦截统一处理。
为什么使用:使用SiteMesh能够在Web开发过程中减少页面文件冗余以及更好的规范统一样式,同时SiteMesh有很好的泛用性,不管用什么方式生成网页内容(static files, Servlet, JSPs, other filters, MVC frameworks 等),只要是由Servlet引擎提供服务的都可以使用SiteMesh框架。
整合流程:
引入依赖
开启SiteMesh过滤器(SpringBoot项目使用Java配置方式,可略过此步骤)
创建装饰模板
自定义配置
SpringBoot项目添加decorator模板页面的Controller
一、引入依赖
如果是maven项目
org.sitemesh
sitemesh
3.0.1
二、开启SiteMesh 过滤器(Java配置方式可略过此步骤)
XML方式,添加SiteMesh Filter 到 /WEB-INF/web.xml,如果为Springboot项目可以使用Java配置方式
...
sitemesh
org.sitemesh.config.ConfigurableSiteMeshFilter
sitemesh
/*
三、创建装饰模板decorator
装饰模板包含了通用的布局以及样式,最基本的模板必须包括以下内容:
标签将由SiteMesh拦截器进行重写覆盖成response返回的内容,SiteMesh允许使用者自定义标签,详见 附录三 、附录四
下面是有内容的案例模板:
SiteMesh example:/* Some CSS */
body { font-family: arial, sans-serif; background-color: #ffffcc; }
h1, h2, h3, h4 { text-align: center; background-color: #ccffcc;
border-top: 1px solid #66ff66; }
.mainBody { padding: 10px; border: 1px solid #555555; }
.disclaimer { text-align: center; border-top: 1px solid #cccccc;
margin-top: 40px; color: #666666; font-size: smaller; }
SiteMesh example site:
四、自定义配置
XML配置方式:在WEB-INF目录下添加sitemesh3.xml文件,path表示拦截请求的规则,decorator指向装饰模板文件,详细配置规则见 附录一
Java配置方式:SiteMesh3还支持Java接口的配置方式,Java配置能满足深度定制需求,同时可以被更高级的语言如JRuby, Groovy, Scala使用
@Configuration //SpringBoot需要配置,将类交给Spring管理
@WebFilter(filterName = "sitemesh",urlPatterns = "/*") //SpringBoot需要配置,代替XML方式开启过滤器
public class MySiteMeshFilter extends ConfigurableSiteMeshFilter {
@Override
protected void applyCustomConfiguration(SiteMeshFilterBuilder builder) {
builder.addDecoratorPath("/*", "/layout/default.html");
}
}
注意:SiteMesh3支持XML和Java接口模式一起使用,加载顺序是先XML后Java接口
五、SpringBoot项目需要添加decorator模板页面的Controller
@Controller
@RequestMapping("/layout")
public class DecoratorController {
@GetMapping("/default")
public String defaultDecorator(){
return "layout/default";
}
}
附录一、详细配置文件规则
XML
/articles/*
/decorators/article.html
/decorators/two-page-layout.html
/decorators/common.html
Java
public class MySiteMeshFilter extends ConfigurableSiteMeshFilter {
@Override
protected void applyCustomConfiguration(SiteMeshFilterBuilder builder) {
// Map default decorator. This shall be applied to all paths if no other paths match.
builder.addDecoratorPath("/*", "/default-decorator.html")
// Map decorators to path patterns.
.addDecoratorPath("/admin/*", "/another-decorator.html")
.addDecoratorPath("/*.special.jsp", "/special-decorator.html")
// Map multiple decorators to the a single path.
.addDecoratorPaths("/articles/*", "/decorators/article.html",
"/decoratos/two-page-layout.html",
"/decorators/common.html")
// Exclude path from decoration.
.addExcludedPath("/javadoc/*")
.addExcludedPath("/brochures/*");
}
}
附录二、配置 MIME Types
By default, SiteMesh will only intercept responses that set the Content-Type HTTP header to text/html.
This can be altered to allow SiteMesh to intercept responses for other types. This is only applicable for the SiteMesh Filter - it is ignored by the offline site builder.
XML
text/html
application/vnd.wap.xhtml+xml
application/xhtml+xml
...
Java
public class MySiteMeshFilter extends ConfigurableSiteMeshFilter {
@Override
protected void applyCustomConfiguration(SiteMeshFilterBuilder builder) {
builder.setMimeTypes("text/html", "application/xhtml+xml", "application/vnd.wap.xhtml+xml");
}
}
附录三、添加自定义标签规则
public class CustomTagRuleBundle implements TagRuleBundle{
@Override
public void install(State defaultState, ContentProperty contentProperty, SiteMeshContext siteMeshContext) {
defaultState.addRule("myScript", new ExportTagToContentRule(siteMeshContext,
contentProperty.getChild("myScript"), false));
defaultState.addRule("myContent", new ExportTagToContentRule(siteMeshContext,
contentProperty.getChild("myContent"), false));
defaultState.addRule("myStyle", new ExportTagToContentRule(siteMeshContext,
contentProperty.getChild("myStyle"), false));
}
@Override
public void cleanUp(State defaultState, ContentProperty contentProperty, SiteMeshContext siteMeshContext) {
}
}
附录四、绑定自定义标签规则
XML方式
...
Java方式
public class MySiteMeshFilter extends ConfigurableSiteMeshFilter {
@Override
protected void applyCustomConfiguration(SiteMeshFilterBuilder builder) {
builder.addTagRuleBundles(new CssCompressingBundle(), new LinkRewritingBundle());
}
}