• springboot使用Freemarker继承


    最近需要用到Freemarker的继承。但是发现没有关于springboot配置Freemarker的继承的。所以趁现在有时间写个博客。


    1. Freemarker继承介绍

    Freemarker 通过 rapid java实现继承。实际是rapid的jar包提供的三个自定义标签。实现继承用到的有三个标签:@extends@block ,@override
    他们三个都有一个共同的属性: name

    @extend标签: 要继承的模板

    @block 标签: 声明在一个模板中定义那些代码是可以被重写的(@ovrride)

    @override标签: 选择要重写的代码块

    2. 依赖配置

    我选择的是maven的依赖

      <!--rapid-framework 模板继承框架-->
            <dependency>
                <groupId>com.googlecode.rapid-framework</groupId>
                <artifactId>rapid-core</artifactId>
                <version>4.0.5</version>
            </dependency>
        <!-- lang包 缺少的话可能会报错  -->
        <dependency>  
          <groupId>commons-lang</groupId>  
          <artifactId>commons-lang</artifactId>  
          <version>2.6</version>  
        </dependency>

    3.Freemarker配置

    application.yml的配置:

    spring:
      freemarker:
        charset: UTF-8
        check-template-location: true
        template-loader-path: classpath:/templates

    在java中的配置,通过@Configuration注解创建配置类,将自定义标签添加进去

    刚开始我引入jar包的时候告诉我找不到该类。但是我在idea中下载源码后就可以找到这些类了。不知道为什么

    import cn.org.rapid_framework.freemarker.directive.BlockDirective;
    import cn.org.rapid_framework.freemarker.directive.ExtendsDirective;
    import cn.org.rapid_framework.freemarker.directive.OverrideDirective;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.context.annotation.Configuration;
    /**
     * @Author LiuYinXin
     * Created at 2017/5/2.21:21.
     */
    @Configuration
    public class FreemarkerConfig {
        @Autowired
        freemarker.template.Configuration configuration;
    
        @PostConstruct
        public void setSharedVariable(){
            configuration.setSharedVariable("block", new BlockDirective());
            configuration.setSharedVariable("override", new OverrideDirective());
            configuration.setSharedVariable("extends", new ExtendsDirective());
        }
    }

    4 模板继承

    创建父模板base.ftl

    <!DOCTYPE html>
    <html lang="en">  
    <head>  
        <meta charset="utf-8"/> 
        <title>
            <@block name="title" >父模板的 title</@block>
        </title>  
    </head>  
    <body>  
        <div>  
            <h3>
                <@block name="body" >父模板的 body</@block>
            </h3>  
        </div>  
    </body>  
    </html>  

    创建son.ftl

    <@override name="title">   
        子模版的 title  
    </@override>  
    
    <@override name="body">  
        子模版的 body  
    </@override>  
    <!--继承的模板要写在最下面-->
    <@extends name="base.ftl"/>  

    这样就搞定了Freemarker继承

    5 致谢

    Freemarker 实现 继承、覆盖 — 趙大叔

    spring 整合freemarker 实现模板继承—阿伦·艾

    关注我,抽搐性更新

    小猿日常

    小猿日常 
    我的公众号,抽搐性更新日常。(突然想发上来。虽然没怎么发布过文章)

    原文 http://blog.csdn.net/liuyinxinall/article/details/71159929

  • 相关阅读:
    css相关
    文章管理列表
    高性能MySQL基础篇
    mysql
    node.js开发实战
    React Hooks
    client-*,scroll-*,offset-*的区别
    将create-react-app从javascript迁移到typescript
    为Github项目添加Travis持续集成服务
    cookie储存
  • 原文地址:https://www.cnblogs.com/gantoday/p/7854096.html
Copyright © 2020-2023  润新知