springboot集成sitemesh是什么-创新互联

这篇文章给大家分享的是有关spring boot集成sitemesh是什么的内容。小编觉得挺实用的,因此分享给大家做个参考。一起跟随小编过来看看吧。

目前成都创新互联已为上1000+的企业提供了网站建设、域名、网站空间、网站托管维护、企业网站设计、伊吾网站维护等服务,公司将坚持客户导向、应用为本的策略,正道将秉承"和谐、参与、激情"的文化,与客户和合作伙伴齐心协力一起成长,共同发展。

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='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>

<h2>我是test</h2>

</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">

<h2>我是test</h2>

</div>

</body>

</html>

感谢各位的阅读!关于spring boot集成sitemesh是什么就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到吧!

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。

本文标题:springboot集成sitemesh是什么-创新互联
当前地址:https://www.cdcxhl.com/article42/csogec.html

成都网站建设公司_创新互联,为您提供App设计面包屑导航Google服务器托管网站设计公司网站维护

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

网站托管运营