工程目录
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