FreeMarker 跟 Thymeleaf 一样,是一种模板引擎,他可以无缝兼容 FreeMarker 在 Spring Boot 开发者中仍然有着很高的地位。

本章重点内容

  1. 编写一个最简单的 Freemark 模板示例
  2. 简单说明 FreeMarker

本项目源码下载

1 FreeMarker 简介

相对于 Jsp ,FreeMarker 具有太多的优势。FreeMarker 适合 Mvc 场景。

FreeMarker 最大的特点就是具有可编程能力,可以对任何后台输出的数据做编程能力,这就像在 Java 中加入了 PHP 功能,这非常有趣。

FreeMarker 支持各类语法包括 字符输出、条件判断 if/else、循环遍历、

1.1 变量

${...}

1.2 条件语句

<#if condition>
...
<#elseif condition2>
...
<#elseif condition3>
...
<#else>
...
</#if>

1.3 循环语句

假设 users 包含['Joe', 'Kate', 'Fred'] 序列:
<#list users as user>
<p>${user}
</#list> 输出:
<p>Joe
<p>Kate
<p>Fred

1.4 include 包含语句

将版权信息单独存放在页面文件 copyright_footer.html 中:
<hr>
<i>
Copyright (c) 2000 <a href="http://www.baidu.com">Baidu Inc</a>,
<br>
All Rights Reserved.
</i> 当需要用到这个文件时,可以使用 include 指令来插入:
<html>
<head>
<title>Test page</title>
</head>
<body>
<h1>Test page</h1>
<p>Blah blah...
<#include "/copyright_footer.html">
</body>
</html>

2 Spring Boot 中编写一个 FreeMarker 示例

本示例文件结构,新增了连个用于示例文件的文件 IndexController.java 与 index.ftl

+ java/fishpro/freemarker/controller
-- IndexController.java
+ resources/templates
--index.ftl

2.1 新建一个 Spring Boot 的 Maven 项目

2.1 新建 Spring Boot 项目

  1. File > New > Project,如下图选择 Spring Initializr 然后点击 【Next】下一步
  2. 填写 GroupId(包名)、Artifact(项目名) 即可。点击 下一步

    groupId=com.fishpro

    artifactId=freemarker
  3. 选择依赖 Spring Web Starter 前面打钩,在模板列中勾选 apache freemarker
  4. 项目名设置为 spring-boot-study-freemarker.

2.2 编辑 Pom.xml 引入依赖

注意如果在新建下面的时候没有引入 FreeMarker 那么就在这里复制粘贴上去。

<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.fishpro</groupId>
<artifactId>freemarker</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>freemarker</name>
<description>Demo project for Spring Boot</description> <properties>
<java.version>1.8</java.version>
</properties> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build> </project>

2.3 配置文件

注意 Spring Boot 中默认不需要做任何配置,示例程序把默认的端口 8080 改为了 8086,并把 application.properties 改为了 application.yml

server:
port: 8086

2.4 编写示例代码

本示例文件结构,新增了连个用于示例文件的文件 IndexController.java 与 index.ftl

IndexController.java

/**
* 在是一个普通的 Controller 类
* */
@Controller
public class IndexController { /**
* 路由 /index
* 返回 index 这里默认配置自动映射到 templages/index
* */
@RequestMapping("/index")
public String index(Model model){
model.addAttribute("welcome","hello fishpro");
return "index";
}
}

index.ftl

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
this is welcome ${welcome}
</body>
</html>

2.5 运行示例

在浏览器中输入 http://localhost:8086/index 显示为,hello fishpro 就是后台输出的 model 对象

this is welcome hello fishpro

2.6 FreeMarker 实际应用场景

FreeMarker 比传统的 JSP 多出一个模板的作用,就是说,我们可以做多个不同样式的模板,动态的切换。这就是 FreeMarker 应用场景。

本项目源码下载

Spring Boot FreeMarker 使用教程的更多相关文章

  1. spring boot+freemarker+spring security标签权限判断

    spring boot+freemarker+spring security标签权限判断 SpringBoot+SpringSecurity+Freemarker项目中在页面上使用security标签 ...

  2. Spring boot Freemarker 获取ContextPath的方法

    Spring boot Freemarker 获取ContextPath的两种方法: 1.自定义viewResolver,Spring boot中有一个viewResolver,这个和配置文件中的师徒 ...

  3. Spring Boot Freemarker特别篇之contextPath【从零开始学Spring Boot】(转)

    需求缘起:有人在群里@我:请教群主大神一个问题,spring boot  + freemarker 怎么获取contextPath 头疼死我了,网上没一个靠谱的 .我就看看之前博客中的 [Spring ...

  4. Spring Boot Freemarker特别篇之contextPath【从零开始学Spring Boot

      需求缘起:有人在群里@我:请教群主大神一个问题,spring boot  + freemarker 怎么获取contextPath 头疼死我了,网上没一个靠谱的 .我就看看之前博客中的 [Spri ...

  5. Spring Boot Mybatis 使用教程

    Mybatis 在当下互联网开发环境,十分重要.本章主要讲述 Mybatis 如何使用. 从本系列开始,都需要用到 mysql 数据库 和其他一些参考的数据库.请准备相关环节.本章需要以下环境支撑: ...

  6. Spring Boot JPA 使用教程

    JPA 是 Spring Boot 官方推荐的数据库访问组件,其充分体现了面向对象编程思想,有点像 asp.net 的 EFCore.JPA 也是众多 ORM 的抽象. 从本系列开始,都需要用到 my ...

  7. Spring Boot 2.x教程-Thymeleaf 原理是什么

    layout: post title: Spring Boot 2.x教程-Thymeleaf 原理是什么 categories: SpringBoot description: Spring Boo ...

  8. Spring Boot 2 快速教程:WebFlux 集成 Thymeleaf(五)

    号外:为读者持续整理了几份最新教程,覆盖了 Spring Boot.Spring Cloud.微服务架构等PDF.获取方式:关注右侧公众号"泥瓦匠BYSocket",来领取吧! 摘 ...

  9. Spring Boot + Freemarker多语言国际化的实现

    最近在写一些Web的东西,技术上采用了Spring Boot + Bootstrap + jQuery + Freemarker.过程中查了大量的资料,也感受到了前端技术的分裂,每种东西都有N种实现, ...

随机推荐

  1. c++中sort函数调用报错Expression : invalid operator <的内部原理 及解决办法

    转自:https://www.cnblogs.com/huoyao/p/4248925.html 当我们调用sort函数进行排序时,中的比较函数如果写成如下 bool cmp(const int &a ...

  2. 12.动态内存和智能指针、 直接管理内存、shared_ptr和new结合使用

    12.动态内存和智能指针 1.智能指针分为两种shared_ptr和unique_ptr,后者独占所指向的对象.智能指针也是模板,使用时要用尖括号指明指向的类型.类似emplace成员,make_sh ...

  3. 【NS-3学习】ns3-模拟基础:关键概念,日志,命令行参数

    前言 本篇博客先介绍在仿真过程中会使用到的一些关键概念,然后介绍便于调试仿真脚本的常用技术:日志.命令行参数. 关键概念 节点 在因特网术语中,主机(终端)是指任何一台连接到网络的计算设备.ns-3并 ...

  4. c++基础语法规则

    1,c++存储类:定义函数或者变量的生命周期     auto 关键字用于两种情况:声明变量时根据初始化表达式自动推断该变量的类型.声明函数时函数返回值的占位符. register 存储类用于定义存储 ...

  5. Codeforces Round #598 (Div. 3) F. Equalizing Two Strings

    You are given two strings ss and tt both of length nn and both consisting of lowercase Latin letters ...

  6. 题解【洛谷P5019】[NOIP2018]铺设道路

    题目描述 春春是一名道路工程师,负责铺设一条长度为 \(n\) 的道路. 铺设道路的主要工作是填平下陷的地表.整段道路可以看作是 \(n\) 块首尾相连的区域,一开始,第 \(i\) 块区域下陷的深度 ...

  7. 安卓按键:读取txt开头出现未知字符的问题

    很多时候 我们读取txt 用traceprint输出后 最头上会莫名其妙多出一个问号 但是你用问号匹配他 却匹配不到  就是1个未知字符  这个到底是什么 怎么避免出现这个东西呢 这个主要是txt文件 ...

  8. 505,display,float,position之间的关系(有疑问)

    (display属性设置元素如何显示) 如果display取值为none,那么position和float都不起作用,这种情况下元素不产生框 否则,如果position设置框是绝对定位,float的计 ...

  9. VIM - EX 命令 - 文件读写

    VIM - EX 命令 - 文件读写 1. 概述 vim 通过 ex 命令行, 与其他文件的读写操作 2. 场景 场景1 vim 打开文本 将当前文本的内容, 写入到其他文本 场景2 vim 打开文本 ...

  10. Atcoder Beginner Contest151E(排列组合)

    排列组合 #define HAVE_STRUCT_TIMESPEC #include<bits/stdc++.h> using namespace std; ]; ; ]; long lo ...