springBoot + sitemesh3

依赖的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>

结果
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值