Home >Java >javaTutorial >spring boot integrated sitemesh

spring boot integrated sitemesh

(*-*)浩
(*-*)浩forward
2019-09-04 16:52:423655browse

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:

<dependency>
<groupId>org.sitemesh</groupId>
<artifactId>sitemesh</artifactId>
<version>3.0.1</version>
</dependency>

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>标签

@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.

<!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=&#39;body&#39; />

</div>

</body>

</html>

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

<!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>

The final page is:

<!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>

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

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete