sitemesh框架的简单使用(springboot+maven+jsp+sitemesh)

一 简单介绍

  • sitemesh是一种模板框架,是为了解决页面重复代码而设计的
  • sitemesh的设计思想是装饰者设计模式

二 简单使用

目录结构,因为我这个项目本来是用来学习flowable的,后面为了方便快速学习,直接把sitemesh集成到这里了,读者只需关心下方红框的文件即可
在这里插入图片描述

  1. 引用依赖(由于springboot默认使用的是themeleaf,不支持使用jsp,所以我们需要引入相关支持jsp的依赖,因为公司用的jsp,所以这里也是用jsp进行演示)
		<dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <scope>provided</scope>
        </dependency>
        <!--用于编译jsp-->
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
            <!--<scope>provided</scope>-->
        </dependency>
        <!-- 支持jsp结果 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>org.sitemesh</groupId>
            <artifactId>sitemesh</artifactId>
            <version>3.0.0</version>
        </dependency>
  1. 编写过滤器
    过滤器的作用:拦截所有的请求,默认通过读取/WEB-INF/sitemesh3.xml的方式来进行配置,当然也可以通过写java代码进行配置(后面讲到)
import org.sitemesh.config.ConfigurableSiteMeshFilter;
import org.springframework.context.annotation.Configuration;
import javax.servlet.annotation.WebFilter;

@Configuration
@WebFilter(filterName="SitemeshFilter",urlPatterns="/*")
public class SitemeshFilter extends ConfigurableSiteMeshFilter {
}

附:ConfigurableSiteMeshFilter 部分代码
在这里插入图片描述

  1. 编写resources/application.yml文件
spring:
  mvc:
    view:
      prefix: /WEB-INF/app-jsp/
      suffix: .jsp
  1. 编写sitemesh3.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<sitemesh>

	<!-- 默认装饰器,当下面的路径都不匹配时,启用该装饰器进行装饰 -->
	<mapping decorator="/WEB-INF/app-jsp/decorators/header.jsp"/>

	<!-- 排除,不进行装饰的路径 -->
	<mapping path="/login" exclue="true"/>
	<mapping path="/" exclue="true"/>

	<!-- 对不同的路径,启用不同的装饰器 -->
	<mapping path="/main" decorator="/WEB-INF/app-jsp/decorators/default.jsp" />
<!--	<mapping path="/decoratorsTest" decorator="/WEB-INF/app-jsp/decorators/decorators-test.jsp" />-->


	<!-- 对不同的路径,使用同一个装饰器 -->
	<mapping decorator="/WEB-INF/app-jsp/decorators/header.jsp">
		<path>/decoratorsTest2</path>
		<path>/decoratorsTest3</path>
<!--		<decorator>/WEB-INF/app-jsp/decorators/header.jsp</decorator>-->
	</mapping>

	<!-- 对同一路径,启用多个装饰器 -->
	<!-- 注意事项:当需要对同一个路径配置多个装饰器时,会从上往下进行装饰,也就是先使用default.jsp进行装饰,
	 	然后把装饰后的default.jsp的body作为整体被装饰的内容,再通过decorators-test.jsp进行装饰
	 -->
	<mapping path="/testHb">
<!--		<path>/testHb/*</path>-->
		<decorator>/WEB-INF/app-jsp/decorators/default.jsp</decorator>
		<decorator>/WEB-INF/app-jsp/decorators/decorators-test.jsp</decorator>
	</mapping>

	<!-- Sitemesh3 默认只提供了 body,title,head 等 tag 类型,我们可以通过实现 TagRuleBundle 扩展自定义的 tag 规则 -->
	<content-processor>
		<tag-rule-bundle class="com.yzy.flowable.sitemesh.decorators.DecoratorsTag" />
	</content-processor>

	<!-- 使用自定义的tag -->
	<mapping path="/decoratorsTest4" decorator="/WEB-INF/app-jsp/decorators/myTagTest.jsp" />

</sitemesh>
  1. 编写SitemeshController,新增访问路径login
 @RequestMapping("login")
    public String login(){
        return "login";
    }
login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html>
<head>
    <title>登录页</title>
</head>
<body>
    <h1>我是登录页面</h1>
</body>
</html>

对应sitemesh3.xml文件,访问路径为/login时,不进行装饰.
页面效果:
在这里插入图片描述

  1. 编写SitemeshController,新增访问路径main
    @RequestMapping("main")
    public String main(){
        return "main";
    }
main.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html>
<head>
    <title>主页面</title>
</head>
<body>
    <h1>我是主页面</h1>
</body>
</html>

对应sitemesh3.xml文件,访问路径为/main时,进行装饰.
在这里插入图片描述

default.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html>
<head>
    <title>
        <sitemesh:write property='title'/>
    </title>
</head>
<body>

    <h3>我是默认的页面</h3>
    <hr>
    插入的内容:<br>
    <sitemesh:write property='body'/><br/>

</body>
</html>
页面效果

在这里插入图片描述

分析:因为对/main进行了过滤,main.jsp的<title>和<body>内容被抽取出来,填充到default.jsp对应的位置上,最后,浏览器渲染的是default.jsp的内容

  1. 装饰器其它的配置参考sitemesh3.xml文件
不同的路径,使用同一个装饰器 或者 同一路径,启用多个装饰器等
  1. 自定义tag,参考sitemesh3.xml
	<!-- Sitemesh3 默认只提供了 body,title,head 等 tag 类型,我们可以通过实现 TagRuleBundle 扩展自定义的 tag 规则 -->
	<content-processor>
		<tag-rule-bundle class="com.yzy.flowable.sitemesh.decorators.DecoratorsTag" />
	</content-processor>

	<!-- 使用自定义的tag -->
	<mapping path="/decoratorsTest4" decorator="/WEB-INF/app-jsp/decorators/myTagTest.jsp" />
编写DecoratorsTag.java文件
import org.sitemesh.SiteMeshContext;
import org.sitemesh.content.ContentProperty;
import org.sitemesh.content.tagrules.TagRuleBundle;
import org.sitemesh.content.tagrules.html.ExportTagToContentRule;
import org.sitemesh.tagprocessor.State;

public class DecoratorsTag implements TagRuleBundle {
    @Override
    public void install(State state, ContentProperty contentProperty, SiteMeshContext siteMeshContext) {
        state.addRule("myBody", new ExportTagToContentRule(siteMeshContext,contentProperty.getChild("myBody"), false));
    }

    @Override
    public void cleanUp(State state, ContentProperty contentProperty, SiteMeshContext siteMeshContext) {

    }
}

修改main.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html>
<head>
    <title>主页面</title>
</head>
<body>
    <h1>我是主页面</h1>
    <myBody>我是自定义标签</myBody>
</body>
</html>
myTagTest.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html>
<head>
    <title><sitemesh:write property='title'/></title>
</head>
<body>
    <h3>我是头部</h3>
    <hr>
    插入的内容:<br>
    <sitemesh:write property='myBody'/><br/>

    <!-- 引进尾部 -->
    <jsp:include page="footer.jsp"></jsp:include>
</body>
</html>
页面效果

在这里插入图片描述

可知:myTagTest.jsp在进行装饰main.jsp时,只抽取了<myBody>标签的内容

以上就是sitemesh的简单用法,也足够了

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值