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

时间: 2024-08-30 14:46:38

springboot使用Freemarker继承的相关文章

springboot整合freemarker

前后端分离现在越来越多,如何有效的使用springboot来整合我们的页面是一个很重要的问题. springboot整合freemarker有以下几个步骤,也总结下我所犯的错误: 1.加依赖: 2.配置文件修改: 3.在templates下面编写后缀为ftl的页面: 4.错误出现:404问题: (1)检查是@RestController还是@Controller,如果要返回页面必须用@Controller (2)这次问题出现的很粗心:(漏掉一个小点) 原文地址:https://www.cnblo

SpringBoot学习8:springboot整合freemarker

1.创建maven项目,添加pom依赖 <!--springboot项目依赖的父项目--> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.0.RELEASE</version> </parent> <dep

springboot整合freemarker自动加载宏

springboot引入freemarker最大的问题,在于springboot的application.properties配置文件,不能覆盖所有的freemarker配置 如果freemarker有自定义宏应该怎样引入呢? 首先 application.properties增加配置(与freemarker.properties一样) auto_import = _auto_include = /layout/pageShow.html 然后增加FreemarkerConfig类,启动即可 /

DEMO: springboot 与 freemarker 集成

直接在 DEMO: springboot 与 mybatis 集成 基础上,进行修改. 1.pom.xml 中引用 依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-freemarker</artifactId> <version>1.4.1.RELEASE</version> </d

SpringBoot整合Freemarker+Mybatis

开发工具 , 开始 新建工程 .选择Spring Initializr 下一步 下一步,选择需要的组件 ..改一下工程名,Finish ..目录结构 首先,修改pom文件 然后,将application.properties改成yml文件,并且配置相关参数 我的数据库很简单,user表,int类型的自增id,varchar类型的name. 之后建立各个文件,目录结构如下: HelloController package com.example.controller; import com.exa

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

二.springboot+mybatis的使用 1.springboot的注解:@SpringBootApplication :启动项目:整合常用注解(@Configuration,@EnableAutoConfiguration,@ComponentScan)/扫包作用(只能在当前同级包下)  @EnableAutoConfiguration自动配置  @ComponentScan扫描一些组件如controller.service 可以扫多个包.扫多个包用法@ComponentScan(bas

spring-boot集成Freemarker开发

1.该demo功能http://localhost/system/login展示后台发布新闻列表http://localhost/前端Freemark模板展示中间用到了Mysql, Mybatis, druid;可以参考spring-boot相关的demo 2.Pom依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-freem

maven搭建springboot+mybatis+freemarker

创建maven项目后,在pox.xml中添加依赖的jar包 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.x

SpringBoot入门-15(springboot配置freemarker使用YML)

https://blog.csdn.net/fengsi2009/article/details/78879924 application.yml spring: http: encoding: force: true charset: UTF-8 freemarker: allow-request-override: false cache: false check-template-location: true charset: UTF-8 content-type: text/html;