一 簡單介紹
- sitemesh是一種模板框架,是為了解決頁面重復代碼而設計的
- sitemesh的設計思想是裝飾者設計模式
二 簡單使用
目錄結構,因為我這個項目本來是用來學習flowable的,后面為了方便快速學習,直接把sitemesh集成到這里了,讀者只需關心下方紅框的文件即可
引用依賴(由于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><dependency><groupId>org.apache.tomcat.embed
</groupId><artifactId>tomcat-embed-jasper
</artifactId></dependency><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>
編寫過濾器
過濾器的作用:攔截所有的請求,默認通過讀取/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 部分代碼
編寫resources/application.yml文件
spring:mvc:view:prefix: /WEB
-INF/app
-jsp/
suffix: .jsp
編寫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 decorator="/WEB-INF/app-jsp/decorators/header.jsp"><path>/decoratorsTest2
</path><path>/decoratorsTest3
</path>
</mapping><mapping path="/testHb">
<decorator>/WEB-INF/app-jsp/decorators/default.jsp
</decorator><decorator>/WEB-INF/app-jsp/decorators/decorators-test.jsp
</decorator></mapping><content-processor><tag-rule-bundle class="com.yzy.flowable.sitemesh.decorators.DecoratorsTag" /></content-processor><mapping path="/decoratorsTest4" decorator="/WEB-INF/app-jsp/decorators/myTagTest.jsp" /></sitemesh>
編寫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時,不進行裝飾.
頁面效果:
編寫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的內容
裝飾器其它的配置參考sitemesh3.xml文件
不同的路徑,使用同一個裝飾器 或者 同一路徑,啟用多個裝飾器等
自定義tag,參考sitemesh3.xml
<content-processor><tag-rule-bundle class="com.yzy.flowable.sitemesh.decorators.DecoratorsTag" /></content-processor><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 {@Overridepublic void install(State state
, ContentProperty contentProperty
, SiteMeshContext siteMeshContext
) {state
.addRule("myBody", new ExportTagToContentRule(siteMeshContext
,contentProperty
.getChild("myBody"), false));}@Overridepublic 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的簡單用法,也足夠了
總結
以上是生活随笔為你收集整理的sitemesh框架的简单使用(springboot+maven+jsp+sitemesh)的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。