java页面布局框架_Web网页布局框架SiteMesh3整合详细流程,支持SpringBoot

本文详细介绍了如何整合SiteMesh3框架,支持SpringBoot应用。SiteMesh通过类似拦截器的方式工作,用于减少页面冗余,统一页面样式。整合步骤包括引入依赖、开启过滤器、创建装饰模板、自定义配置以及在SpringBoot项目中添加decorator模板页面的Controller。此外,还提到了XML和Java配置方式,以及自定义标签规则和MIME类型的配置。
摘要由CSDN通过智能技术生成

框架原理:类似于拦截器机制,不影响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:

Site disclaimer. This is an example.

四、自定义配置

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());

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值