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. Zuul整合Swagger,使用ZuulFilter解决下游服务context-path

    问题起因:使用Zuul网关服务,需要整合下游系统的swagger,但是下游服务存在context-path配置,无法正确跳转,最后使用ZuulFilter解决. 1.Zuul整合下游swagger 首 ...

  2. 动图+源码,演示 Java 中常用数据结构执行过程及原理

    ​阅读本文大概需要 3.7 分钟. 作者:大道方圆 cnblogs.com/xdecode/p/9321848.html 最近在整理数据结构方面的知识, 系统化看了下Java中常用数据结构, 突发奇想 ...

  3. 第一次实验报告:使用Packet Tracer分析HTTP数据包

    目录 1 实验目的 2 实验内容 3. 实验报告 第一次实验报告:使用Packet Tracer分析HTTP数据包 1 实验目的 熟练使用Packet Tracer工具.分析抓到的HTTP数据包,深入 ...

  4. Golang(八)go modules 学习

    0. 前言 最近加入鹅厂学习 k8s,组内使用 Go 1.11 以上的 go modules 管理依赖,因此整理了相关资料 本文严重参考原文:初窥Go module 1. 传统 Golang 包依赖管 ...

  5. 获取 Rancher 中 Prometheus 的数据

    1.需求 在 rancher 应用商店添加集群监控,会安装 prometheus.grafana:需要从 prometheus 的 api 中收集 pod 的一些信息. 查看grafana 配置的数据 ...

  6. SQL --------------- between 和< >

    between值 and 值 运算符用于选取介于两个值之间的数据范围内的值,常与where一块使用between运算符选择给定范围内的值.值可以是数字,文本或日期. 使用between的时候会与and ...

  7. 使用AtomicInteger写一个显示锁

    利用了AtomicInteger的compareAndSet方法 public class CASLock { private AtomicInteger value = new AtomicInte ...

  8. SQL忽略重复键作用

    1.插入时如果开启的话,发现重复键会忽略,否则报错 2.更新时不管开启是否都会报错

  9. RDD的转换操作,分三种:单value,双value交互,(k,v)对

    import org.apache.spark.rdd.RDDimport org.apache.spark.{Partitioner, SparkConf, SparkContext} object ...

  10. SoapException: Timed out while processing web services request

    情形:动态调用WebService时,语句method.Invoke异常. 异常信息为调用目标发生异常,从异常信息并不能看出问题所在,需要查看InnerException,如标题所述:处理web请求超 ...