springboot 整合 freemarker

依赖

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
</parent> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>3.9.1</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
</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>
</dependency>
</dependencies>

application.yml

application 参数路径

server:
port: 8001
spring:
application:
name: test-freemarker
freemarker:
cache: false
settings:
template_update_delay: 0
template-loader-path: classpath:/templates/

启动类

@SpringBootApplication
public class FreemarkerApplication {
public static void main(String[] args) {
SpringApplication.run(FreemarkerApplication.class, args);
}
@Bean
public RestTemplate restTemplate(){
return new RestTemplate(new OkHttp3ClientHttpRequestFactory());
}
}

模板文件

<!DOCTYPE html>
<!-- resources/templates/test2.ftl -->
<html>
<head lang="en">
<meta charset="UTF-8"/>
<title></title>
</head>
<body>
<table>
<tr>
<td>序号</td>
<td>姓名</td>
<td>年龄</td>
<td>金钱</td>
<td>出生日期</td>
</tr>
<#if students??>
<#list students as stu>
<tr>
<td>${stu_index}</td>
<td <#if (stu.name == '刘备')>style="background-color: #108cee"</#if> >${stu.name}</td>
<td <#if (stu.age < 20)>style="background-color: #108cee"</#if>>${stu.age}</td>
<td>${stu.money}</td>
<td>${stu.birthday?date},${stu.birthday?time},${stu.birthday?string("yyyy年MM月dd日")}</td>
</tr>
</#list>
</#if>
</table>
姓名:${stuMap['stu1'].name}
年龄: ${stuMap.stu1.age}
<#list stuMap?keys as k>
姓名: ${stuMap[k].name}
年龄: ${stuMap[k].age}
</#list>
${stuMap???c}//判断是否存在,和使用 ?c 输出字符串
${students???c}
${(mozq.bank.address)!'中国建设银行'}//默认值方式处理空值 ${students?size}//集合大小
<#assign text="{'bank':'中国农业银行', 'address':'北大街'}">
<#assign data=text?eval>
开户行: ${data.bank} 地址: ${data.address}
${123456123?c}//不显示逗号分隔
${123456123}//默认显示逗号分隔
</body>
</html>
<!DOCTYPE html>
<!-- resources/templates/index_banner.ftl -->
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div class="banner-roll">
<#if model??>
<#list model as item>
<div class="item" style="background-image: url(${item.value});"></div>
</#list>
</#if>
</div>
</div>
<script type="text/javascript">
//...
</script>
</body>
</html>

Controller

package com.mozq.springboot.freemarker.controller;

import com.mozq.springboot.freemarker.model.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.client.RestTemplate; import java.util.*; @Controller //注意不要使用 @RestController
@RequestMapping("/freemarker")
public class FreeMarkerController { @Autowired
private RestTemplate restTemplate; @RequestMapping("/banner")
public String banner(Map<String,Object> map){
String dataUrl = "http://localhost:31001/cms/config/getmodel/5a791725dd573c3574ee333f";
ResponseEntity<Map> entity = restTemplate.getForEntity(dataUrl, Map.class);
Map body = entity.getBody();
map.putAll((Map<? extends String, ?>) body);
// restTemplate.getForEntity("")
return "index_banner";
} @RequestMapping("/test2")
public String test2(Map<String,Object> map){
Student stu1 = new Student();
stu1.setName("刘备");
stu1.setAge(18);
stu1.setBirthday(new Date());
stu1.setMoney(22225.8F); Student stu2 = new Student();
stu2.setName("孙权");
stu2.setAge(20);
stu2.setBirthday(new Date());
stu2.setMoney(24525.8F);
stu2.setBestFriend(stu1); List<Student> students = new ArrayList<>();
students.add(stu1);
students.add(stu2);
//模板使用的数据
map.put("students", students); HashMap<String, Student> stuMap = new HashMap<>();
stuMap.put("stu1", stu1);
stuMap.put("stu2", stu2);
map.put("stuMap", stuMap);
//返回模板的位置,基于 resources/templates
return "test2";
}
}

springboot 整合 freemarker的更多相关文章

  1. springboot整合freemarker

    前后端分离现在越来越多,如何有效的使用springboot来整合我们的页面是一个很重要的问题. springboot整合freemarker有以下几个步骤,也总结下我所犯的错误: 1.加依赖: 2.配 ...

  2. SpringBoot整合freemarker 引用基础

    原 ElasticSearch学习笔记Ⅲ - SpringBoot整合ES 新建一个SpringBoot项目.添加es的maven坐标如下: <dependency> <groupI ...

  3. 【SpringBoot】09.SpringBoot整合Freemarker

    SpringBoot整合Freemarker 1.修改pom文件,添加坐标freemarker启动器坐标 <project xmlns="http://maven.apache.org ...

  4. SpringBoot学习8:springboot整合freemarker

    1.创建maven项目,添加pom依赖 <!--springboot项目依赖的父项目--> <parent> <groupId>org.springframewor ...

  5. springboot整合freemarker(转)

    添加依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>sp ...

  6. SpringBoot整合Freemarker+Mybatis

    开发工具 , 开始 新建工程 .选择Spring Initializr 下一步 下一步,选择需要的组件 ..改一下工程名,Finish ..目录结构 首先,修改pom文件 然后,将applicatio ...

  7. Spring-Boot整合freemarker引入静态资源css、js等

    一.概述 springboot 默认静态资源访问的路径为:/static 或 /public 或 /resources 或 /META-INF/resources 这样的地址都必须定义在src/mai ...

  8. Spring-Boot整合freemarker引入静态资源css、js等(转)

    一.概述 springboot 默认静态资源访问的路径为:/static 或 /public 或 /resources 或 /META-INF/resources 这样的地址都必须定义在src/mai ...

  9. springboot整合freemarker模板引擎后在页面获取basePath绝对路径

    在项目中引用静态资源文件或者进行ajax请求时我们有时候会使用 ${basePath} ,其实这就是一种获取绝对路径的方式: 那么在springboot项目中要怎么配置才能使用 basePaht呢? ...

随机推荐

  1. axios详解

    一.说明 Axios是一个基于Promise(ES6中用于处理异步的)的HTTP库(HTTP客户端),用于浏览器和node.js中,API. 浏览器中创建XMLHttpRequests 从node.j ...

  2. K8S 如何实现将git代码下拉到指定的容器路径中

    gitRepo 是 kubernetes Volume类型中的一种,gitRepo volume可以实现将git代码下拉到指定的容器路径中. 备注:实现此功能,Pod运行的节点都必需要安装git.换句 ...

  3. 基于 Docker 实现 DevOps 的一些探索

    DevOps 介绍 DevOps(Deveplopment 和 Operations 的简称),中译为开发运维一体化,可定义为是一种过程.方法.文化.运动或实践,主要是为了通过一条高度自动化的流水线来 ...

  4. Gin框架 - 项目目录

    概述 今天给大家分享,在 API 端使用 Gin 框架时,项目的目录. 目录 ├─ Project Name │ ├─ config //配置文件 │ ├── ... │ ├─ controller ...

  5. Python转义序列

    正则表达式参考:https://www.cnblogs.com/huxi/archive/2010/07/04/1771073.html

  6. Java学习:JDK8的新特性

    Java学习:JDK8的新特性 一.十大特性 Lambda表达式 Stream函数式操作流元素集合 接口新增:默认方法与静态方法 方法引用,与Lambda表达式联合使用 引入重复注解 类型注解 最新的 ...

  7. Filebeat与Logstash配置SSL加密通信

    为了保证应用日志数据的传输安全,我们可以使用SSL相互身份验证来保护Filebeat和Logstash之间的连接. 这可以确保Filebeat仅将加密数据发送到受信任的Logstash服务器,并确保L ...

  8. mac 远程桌面连接

    1.安装 百度搜索下载远程桌面连接软件 microsoft remote desktop for mac,或者你安装了mac版的office,会默认有安装这个软件的. 提示“证书或相关链无效”的解决办 ...

  9. SQL Server 2014查看服务器数据库字段报错 (Microsoft.SqlServer.Management.Sdk.Sfc)

    报错信息 无法为该请求检索数据. (Microsoft.SqlServer.Management.Sdk.Sfc) 未知属性 IsMemoryOptimized (Microsoft.SqlServe ...

  10. 基于注解的SpringAOP源码解析(二)

    在上篇文章 中我们搭建了一个阅读源码的demo工程,然后简单介绍了一下@EnableAspectJAutoProxy注解,这个注解最重要的功能就是为向Spring中注入了一个beanAnnotatio ...