最近需要用到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

springboot使用Freemarker继承的更多相关文章

  1. springboot整合freemarker

    前后端分离现在越来越多,如何有效的使用springboot来整合我们的页面是一个很重要的问题. springboot整合freemarker有以下几个步骤,也总结下我所犯的错误: 1.加依赖: 2.配 ...

  2. 记springboot+mybatis+freemarker+bootstrap的使用(1)

    一..springboot的配置 1.安装并配置maven maven是项目管理工具,可以自动下载并管理jar包之间的依赖关系,可通过maven自动配置springboot 参照百度经验https:/ ...

  3. SpringBoot学习8:springboot整合freemarker

    1.创建maven项目,添加pom依赖 <!--springboot项目依赖的父项目--> <parent> <groupId>org.springframewor ...

  4. springboot整合freemarker(转)

    添加依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>sp ...

  5. springboot 使用 freemarker 无法正常跳转的问题?

    1.springboot 使用 freemarker 无法正常跳转的问题? 参考:https://blog.csdn.net/Lin_xiaofeng/article/details/79122053 ...

  6. springboot 整合 freemarker

    springboot 整合 freemarker 依赖 <parent> <groupId>org.springframework.boot</groupId> & ...

  7. SpringBoot整合freemarker 引用基础

    原 ElasticSearch学习笔记Ⅲ - SpringBoot整合ES 新建一个SpringBoot项目.添加es的maven坐标如下: <dependency> <groupI ...

  8. 【SpringBoot】09.SpringBoot整合Freemarker

    SpringBoot整合Freemarker 1.修改pom文件,添加坐标freemarker启动器坐标 <project xmlns="http://maven.apache.org ...

  9. 基于springboot的freemarker创建指定格式的word文档

    在web或其他应用中,经常我们需要导出或者预览word文档,比较实际的例子有招聘网站上预览或者导出个人简历,使用POI导出excel会非常的方便,但是如果想导出word,由于其格式控制非常复杂,故而使 ...

随机推荐

  1. [Erlang07] Erlang 做图形化编程的尝试:纯Erlang做2048游戏

    用Erlang久了,以为erlang做类似于As3,JS的图形化界面是绝对不可能的,多少次,多少次想用erlang做个炫酷的图形游戏.终于:折腾出来了结果:纯Erlang也可以做到! 因为以前接触过W ...

  2. spring boot 错误:Check your ViewResolver setup

    Whitelabel Error Page This application has no explicit mapping for /error, so you are seeing this as ...

  3. Zeal - 开源离线开发文档浏览器

    https://zealdocs.org/ win10上暂时安装版会crash,请用portalable的解压版

  4. SQL Server分页查询存储过程

    --分页存储过程create PROCEDURE [dbo].[commonPagination]@columns varchar(500), --要显示的列名,用逗号隔开 @tableName va ...

  5. How to Mount a Remote Folder using SSH on Ubuntu

    Connecting to a server across the internet is much more secure using SSH. There is a way that you ca ...

  6. php对象在内存中存在形式

    1,php中基本数据类型存在于栈区,对象这种数据类型存在于堆区: 比如如下代码,两个指针指向同一个对象,其输出相同 <?php /** * Created by PhpStorm. * User ...

  7. PyQt5(3)——信号和槽初探

    信号和槽是QT的核心机制(当信号发射时,链接的槽函数会自动执行) 在pyqt5中信号和槽通过 QObject.signal.connect()链接. QObject类或子类(QWidget)派生的类都 ...

  8. python常用模块之OS

    os模块偏于文件目录管理 <1>.常用方法 工作目录: os.getcwd() 返回当前工作目录 os.chdir(dir) 更改当前工作目录,相当于cd 目录文件操作: os.mkdir ...

  9. js 平均分割

    let alllist=res.data; var result = []; for (var i = 0; i < alllist.length; i += 3) { result.push( ...

  10. vue-cli起的webpack项目 用localhost可以访问,但是切换到ip就不可以访问

    我用的是vux起的一个项目(移动端,基于vue的),因为是移动端的,需要在手机上测试,发现用http://localhost:8081/访问的挺好的,但是换到ip就访问不了,期初我以为是代理的原因,将 ...