依赖的sitemesh3的jar包
<dependency>
<groupId>org.sitemesh</groupId>
<artifactId>sitemesh</artifactId>
<version>3.0.1</version>
</dependency>
先定义拦截器Fileter的bean
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 为自定义的拦截规则
Meshsite3Filter siteMeshFilter = new Meshsite3Filter();
fitler.setFilter(siteMeshFilter);
return fitler;
}
}
再自定义拦截规则
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())//自定义标签
;
}
}
还可以自定义标签
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;
/**
* 自定义一个标签, myTag
*
* @author xiaojf 2017/12/21 16:09
*/
public class MyTagRuleBundle implements TagRuleBundle {
@Override
public void install(State state, ContentProperty contentProperty, SiteMeshContext siteMeshContext) {
/在模板页这样使用 <sitemesh:write property='myTag'/>
/在内容页 <myTag> 内容 </myTag>
/内容页中的标签包住的内容将被放在 <sitemesh:write property='myTag'/>的位置
state.addRule("myTag", new ExportTagToContentRule(siteMeshContext,contentProperty.getChild("myTag"),false));
}
@Override
public void cleanUp(State state, ContentProperty contentProperty, SiteMeshContext siteMeshContext) {
if (!((ContentProperty)contentProperty.getChild("myTag")).hasValue()) {
((ContentProperty)contentProperty.getChild("myTag")).setValue(contentProperty.getValue());
}
}
}
拦截请求的控制器 将请求分发到sitemesh定义的页面
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";
}
}
默认的模板页
<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页面
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/")
public class MeshsiteController {
@RequestMapping("home")
public String home() {
return "home";
}
}
页面放置位置
内容页
<html>
<head>
<title>标题!</title> //title会被注入到<sitemesh:write property='title'/>中
</head>
<body>
<myTag>
custom mytag! //会被注入到<sitemesh:write property='myTag'/>对应的位置
</myTag>
hello meshsite!
</body>
</html>
结果