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设置文件一样 @Configuration public class WebConfig extends WebMvcConfigurerAdapter{ //注册成bean,就像传统项目spring设置文件中的<bean>标签 @Bean public 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{ @Override protected 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的细致内容,更多请关注ki4网别的相干文章!