您好,欢迎访问一九零五行业门户网

spring boot集成sitemesh

sitemesh简介
sitemesh是由一个基于web页面布局、装饰及与现存web应用整合的框架,是一个装饰器。它能帮助我们在由大量页面工程的项目中创建一致的页面布局和外观,如一致的导航条、一致的banner、一致的版权等。
sitemesh是基于servlet的filter的,它通过截取response,并进行装饰后再交付给客户端。
spring boot 集成 sitemesh
集成要做的工作很简单:
1、引入sitemesh.jar包
2、添加一个配置类及过滤器类
3、新增一个装饰器页面
2.1、引入sitemesh.jar包
在maven的pom文件中引入:
<dependency><groupid>org.sitemesh</groupid><artifactid>sitemesh</artifactid><version>3.0.1</version></dependency>
配置类及过滤器类
配置类如下:
import org.springframework.boot.web.servlet.filterregistrationbean;import org.springframework.context.annotation.bean;import org.springframework.context.annotation.configuration;import org.springframework.web.servlet.config.annotation.webmvcconfigureradapter;//生效配置,使之就像传统项目里sping的xml配置文件一样@configurationpublic class webconfig extends webmvcconfigureradapter{//注册成bean,就像传统项目spring配置文件中的<bean>标签@beanpublic filterregistrationbean sitemeshfilter(){filterregistrationbean fitler = new filterregistrationbean();//实例化一个过滤器类websitemeshfilter sitemeshfilter = new websitemeshfilter();fitler.setfilter(sitemeshfilter);return fitler;}}过滤器类如下:import org.sitemesh.builder.sitemeshfilterbuilder;import org.sitemesh.config.configurablesitemeshfilter;public class websitemeshfilter extends configurablesitemeshfilter{@overrideprotected void applycustomconfiguration(sitemeshfilterbuilder builder) {//除了/admin/index和/admin/login页面外,其他所有/admin/下的页面都被/admin/index页面所装饰builder.adddecoratorpath("/admin/*", "/admin/index").addexcludedpath("/admin/index").addexcludedpath("/admin/login");}}
装饰器页面
装饰器页面就是模板页面,过滤器规则中定义的页面都会被该页面所装饰。
<!doctype html><html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3"><head><title>装饰器页面</title></head><body>...<div id="content"><sitemesh:write property='body' /></div></body></html>
有了上面的装饰器页面,当我们访问被装饰的页面比如/admin/test,展现的内容是装饰器页面+被装饰页面的body元素内的内容,<sitemesh:write property='body' />处会被替换为被装饰页面的body元素内的内容。假设,test页面如下:
<!doctype html><html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3"><head><title>test页面</title></head><body><h1>我是test</h1></body></html>
最终得到的页面是:
<!doctype html><html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3"><head><title>装饰器页面</title></head><body>...<div id="content"><h1>我是test</h1></div></body></html>
以上就是spring boot集成sitemesh的详细内容。
其它类似信息

推荐信息