Sitemesh Introduction
Sitemesh is a framework based on Web page layout, decoration and integration with existing Web applications. It is a decorator. It can help us create consistent page layout and appearance in projects with a large number of page projects, such as consistent navigation bars, consistent banners, consistent copyrights, etc.
SiteMesh is based on Servlet filter. It intercepts the response and decorates it before delivering it to the client.
spring boot integrates sitemesh
The work to be done for integration is very simple:
1. Introduce the sitemesh.jar package
2 , add a configuration class and filter class
3, add a decorator page
2.1, introduce the sitemesh.jar package
Introduce in the maven pom file:
org.sitemesh sitemesh 3.0.1
Configuration class and filter class
The configuration class is as follows:
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 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"); } }
Decorator page
The decorator page is the template page, and the pages defined in the filter rules will be decorated by this page.
With the above decorator page, when we visit the decorated page such as /admin/test, the displayed content is the content within the body element of the decorated page on the decorator page,
The final page is:
The above is the detailed content of spring boot integrated sitemesh. For more information, please follow other related articles on the PHP Chinese website!