Home> Java> javaTutorial> body text

spring boot integrated sitemesh

(*-*)浩
Release: 2019-09-04 16:52:42
forward
3562 people have browsed it

spring boot integrated sitemesh

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 
Copy after login

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"); } }
Copy after login

Decorator page

The decorator page is the template page, and the pages defined in the filter rules will be decorated by this page.

   装饰器页面 
...
Copy after login

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, will be replaced with the content within the body element of the decorated page. Assume that the test page is as follows:

   test页面 

我是test

Copy after login

The final page is:

   装饰器页面 
...

我是test

Copy after login

The above is the detailed content of spring boot integrated sitemesh. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!