最近需要用到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. Android-看操作系统短信应用源码-隐式意图激活短信界面

    选择模拟器Unknown Google Nexus,在选择system_process(系统进程) 操作模拟器的,操作系统短信应用,让操作系统短信打印日志,来查看: 然后就找到来,操作系统短信应用打印 ...

  2. css flex cheat sheet

    .container{ display: -webkit-flex/inline-flex; display: -moz-flex/inline-flex; display: -ms-flex/inl ...

  3. python读写Excel文件(xlrd、xlwr)

    一.首先需要安装第三方库:pip install xlrd 1.打开Excel文件,由于写入时需要copy,所以这里加上保留原格式参数:formatting_info=True excel_file ...

  4. docker-compose 命令

    docker-compose初试及命令基础 以一个简单的lnmp.yaml的配置文件进行讲解docker-compose命令的基础讲解,熟练掌握命令 [root@docker lnmp]# cat l ...

  5. 【海量干货】89页PPT详解微信O2O行业解决方案

    根据腾讯大讲堂提供的信息,整理成了PPT,下载地址: http://yunpan.cn/cZAbTnJXnMymd  访问密码 f36d

  6. django DatabaseFunctions

    from django.db.functions import ... Cast() 转换类型 value = Value.objects.annotate(field_as_float=Cast(' ...

  7. 【题解】 LA3708 Graveyard

    题目大意 在一个圆周长为10000的圆上等距离分布n个雕塑,现在有m个新加入的雕塑(还是要求等距离摆放),问n个雕塑移动的总距离的最小值. Solution 显然必然会有一个雕塑不移动,所以可以直接不 ...

  8. CookieJar和HTTPCookieProcessor

    CookieJar和HTTPCookieProcessor 我们在使用爬虫的时候,经常会用到cookie进行模拟登陆和访问.在使用urllib库做爬虫,我们需要借助http.cookiejar库中的C ...

  9. 870. Advantage Shuffle

    Given two arrays A and B of equal size, the advantage of A with respect to B is the number of indice ...

  10. struts2官方 中文教程 系列二:Hello World项目

    先贴个本帖的地址,免得其它网站被爬去了struts2入门系列二之Hello World  即 http://www.cnblogs.com/linghaoxinpian/p/6898779.html ...