工程目录
pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>springboot-demo</artifactId> <groupId>cn.xiaojf</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>springboot-sitemesh3</artifactId> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-freemarker</artifactId> </dependency> <dependency> <groupId>org.sitemesh</groupId> <artifactId>sitemesh</artifactId> <version>3.0.1</version> </dependency> </dependencies> </project>
自定义过滤器,配置拦截规则,
Meshsite3Filter.java
package cn.xiaojf.springboot.sitemesh3.filter; import cn.xiaojf.springboot.sitemesh3.tagrule.MyTagRuleBundle; import org.sitemesh.builder.SiteMeshFilterBuilder; import org.sitemesh.config.ConfigurableSiteMeshFilter; /** * sitemesh 自定义配置 * @author xiaojf 2017/12/21 16:12 */ public class Meshsite3Filter extends ConfigurableSiteMeshFilter { @Override protected void applyCustomConfiguration(SiteMeshFilterBuilder builder) { builder.addDecoratorPath("/*", "/decorator/default")//拦截规则,/decorator/default 会被转发 .addExcludedPath("/static/**") //白名单 .addTagRuleBundle(new MyTagRuleBundle())//自定义标签 ; } }
加入web过滤器
WebConfigure.java
package cn.xiaojf.springboot.sitemesh3.configure; import cn.xiaojf.springboot.sitemesh3.filter.Meshsite3Filter; 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; @Configuration public class WebConfigure extends WebMvcConfigurerAdapter { @Bean public FilterRegistrationBean siteMeshFilter() { FilterRegistrationBean fitler = new FilterRegistrationBean(); Meshsite3Filter siteMeshFilter = new Meshsite3Filter(); fitler.setFilter(siteMeshFilter); return fitler; } }
编写web controller
DecoratorController.java
package cn.xiaojf.springboot.sitemesh3.web.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller @RequestMapping("/decorator") public class DecoratorController { @RequestMapping("default") public String defaultDecorator() { return "/decorator/default"; } }
MeshsiteController.java
package cn.xiaojf.springboot.sitemesh3.web.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller @RequestMapping("/") public class MeshsiteController { @RequestMapping("home") public String home() { return "home"; } }
编写母版文件
default.html
<html> <head> <title> <sitemesh:write property='title'/> </title> <sitemesh:write property='head'/> </head> <body> title的内容 <sitemesh:write property='title'/><br/> body的内容 <sitemesh:write property='body'/><br/> myTag的内容 <sitemesh:write property='myTag'/><br/> </body> </html>
编写被修饰文件 home.html
<html> <head> <title>title</title> </head> <body> <myTag> custom mytag </myTag> hello meshsite </body> </html>
查看效果
http://localhost:8010/home
源码地址
https://gitee.com/xiaojf/springboot-demo/tree/master/springboot-sitemesh3
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架