最近需要用到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. Spring学习(三)——集成 Velocity

    上篇文章http://www.cnblogs.com/wenjingu/p/3822989.html我们使用Gradle构建了一个简单的Spring MVC Web应用程序, 本篇将在上篇的基础上将j ...

  2. SQLite Mysql 模糊查找(like)

    select UserId,UserName,Name,Sex,Birthday,Height,Weight,Role from xqhit_Users where UserName like &qu ...

  3. Django Managers管理器

    Managers class Manager 管理器是向Django模型提供数据库查询操作的接口.Django应用程序中每个模型至少有一个管理器. Manager names 默认情况下管理器的名字为 ...

  4. 二十五、MongoDB 索引 和 explain 的使用

    一.索引基础 索引是对数据库表中一列或多列的值进行排序的一种结构,可以让我们查询数据库变得更快.MongoDB 的索引几乎与传统的关系型数据库一模一样,这其中也包括一些基本的查询优化技巧.创建索引的命 ...

  5. iOS通过dSYM文件分析crash

    重点是dwarfdump --uuid命令 我们在iOS开发中会碰到的很多crash问题,如果Debug调试模式的话,我们可以往往很容易的根据log的输出定位到导致crash的原因,但对于已经上线的应 ...

  6. 爬虫开发.2urllib模块

    一.urllib库 概念:urllib是Python自带的一个用于爬虫的库,其主要作用就是可以通过代码模拟浏览器发送请求.其常被用到的子模块在Python3中的为urllib.request和urll ...

  7. day11学python 多线程+queue

    多线程+queue 两种定义线程方法 1调用threading.Thread(target=目标函数,args=(目标函数的传输内容))(简洁方便) 2创建一个类继承与(threading.Threa ...

  8. django重写form表单中的局部钩子函数

    from django import forms from django.core.exceptions import ValidationError from jax import models c ...

  9. ocp最新考试题库:052新考题及答案整理-36

    36.Which two are true about roles? A) A role can be granted a combination of system and object privi ...

  10. [ActionScript 3.0] 如何获得实例对象的类名及类

    package { import flash.display.DisplayObject; import flash.display.MovieClip; import flash.display.S ...