登录  /  注册
首页 > Java > java教程 > 正文

spring boot集成sitemesh

(*-*)浩
发布: 2019-09-04 16:52:42
转载
3407人浏览过

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>
登录后复制

配置类及过滤器类

配置类如下:

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

</div>

</body>

</html>
登录后复制

有了上面的装饰器页面,当我们访问被装饰的页面比如/admin/test,展现的内容是装饰器页面+被装饰页面的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的详细内容,更多请关注php中文网其它相关文章!

智能AI问答
PHP中文网智能助手能迅速回答你的编程问题,提供实时的代码和解决方案,帮助你解决各种难题。不仅如此,它还能提供编程资源和学习指导,帮助你快速提升编程技能。无论你是初学者还是专业人士,AI智能助手都能成为你的可靠助手,助力你在编程领域取得更大的成就。
来源:CSDN网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
关于CSS思维导图的课件在哪? 课件
凡人来自于2024-04-16 10:10:18
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2024 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号