日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

spring boot 集成 sitemesh

發布時間:2023/12/20 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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>

2.2、配置類及過濾器類

配置類如下:

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");

}

}

2.3、裝飾器頁面

裝飾器頁面就是模板頁面,過濾器規則中定義的頁面都會被該頁面所裝飾。

<!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的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。