最近需要用到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. xml构建

    <a target="_blank" href="http://wpa.qq.com/msgrd?v=3&uin=346252320&site=qq ...

  2. java 异步调用与多线程

    异步与多线程的区别 一.异步和多线程有什么区别?其实,异步是目的,而多 线程是实现这个目的的方法.异步是说,A发起一个操作后(一般都是比较耗时的操作,如果不耗时的操作 就没有必要异步了),可以继续自顾 ...

  3. 折腾了两天的跨站脚本提交问题,与IIS7有关

    根据这里提供的方法,本地测试通过没有问题,但是部署到服务器上之后,只有GET请求可以跨站提交,POST请求继续报错,折腾了两天之后觉得,是不是IIS7的问题?果然,找到了这篇文章,照做之后解决.

  4. Java 连接、操控数据库总结(JDBC)

    看到数据库连接不由得想起了大一末参加团队考核时的悲催经历~~,还记得当初傻傻地按照书本的代码打到 Eclipse 上,然后一运行就各种报错...报错后还傻傻地和书本的代码一遍又一遍地进行核对,发现无误 ...

  5. Data Base Mysql迁移到SqlServer 2008工具使用方法

    Data Base  Mysql迁移到SqlServer 2008工具使用方法 一.下载及安装: 二.

  6. [原创] 思维导图笔记(二):SQL

    为了补一补知识体系上的缺漏,最近找了看完了Ben Forta写的<Sams Teach Yourself SQL in 10 Minutes>,翻译过来叫<SQL必知必会>.从 ...

  7. webservice简单例子

    1.添加web服务. /// <summary> /// demo 的摘要说明 /// </summary> [WebService(Namespace = "htt ...

  8. OpenCV库文件介绍

    以前都是直接用opencv,都没有仔细研究过,这次把库文件都介绍一下. 1.build和source 当我们安装完opencv中,你会发现在opencv文件夹中有两个文件夹,build和source, ...

  9. Origin如何使曲面变平滑?

    在“绘图属性” - “绘图细节”窗口中选中 Layer1 下的曲面数据,“Colormap/Contours” 选项下有“Level”标签,选中并设置“Minor Levels”,将其数值调大即可.

  10. 爬虫开发3.requests模块

    requests模块 - 基于如下5点展开requests模块的学习 什么是requests模块 requests模块是python中原生的基于网络请求的模块,其主要作用是用来模拟浏览器发起请求.功能 ...