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. DVWA XSS (Reflected) 通关教程

    XSS 介绍XSS,全称Cross Site Scripting,即跨站脚本攻击,某种意义上也是一种注入攻击,是指攻击者在页面中注入恶意的脚本代码,当受害者访问该页面时,恶意代码会在其浏览器上执行,需 ...

  2. Eureka工作原理分析

    demo源码地址:https://github.com/flyingJiang/SpringCloud 首先,单独使用Eureka. Eureka整合Ribbon 未完待续……

  3. Windows / Office - KMS激活

    Windows / Office - KMS激活 支持Windows操作系统,支持Office软件:包括Windows 10,Office 2016:包括VL版本和MSDN版. (UPDATE: Of ...

  4. 19条常用的MySQL优化方法(转)

    本文我们来谈谈项目中常用的MySQL优化方法,共19条,具体如下:1.EXPLAIN命令做MySQL优化,我们要善用EXPLAIN查看SQL执行计划.下面来个简单的示例,标注(1.2.3.4.5)我们 ...

  5. Redis学习之Redis服务器数据库实现

    本文内容: 1.Redis服务器保存数据库的方法 2.客户端切换数据库的方法 3.数据库保存键值对的方法 4.数据库的添加,删除,查看,更新操作的实现方法 5.服务器保存键的过期时间的方法 6.服务器 ...

  6. vue基础之data

    使用 调用data onLoad(option) { _self = this; _self.$data.xxxx = "te"; } 绑定节点 元素~~~~ <input ...

  7. 【转】Visual Studio Code(VS code)你们都在用吗?或许你们需要看一下这篇博文

    写在前面 在前端开发中,有一个非常好用的工具,Visual Studio Code,简称VS code. 都不用我安利VS code,大家就会乖乖的去用,无数个大言不惭的攻城狮,都被VS code比德 ...

  8. C#下载csv代码总结

    一.C#导出csv格式代码如下: /// <summary> /// 下载 /// </summary> /// <param name="startTime& ...

  9. Spring MVC处理参数Convert

    Springmvc.xml 配置convert,xml中配置多个相同的泛型时,xml里配置的convert会从上到下挨个执行. <!-- 配置注解驱动,并配置convert --> < ...

  10. G++命令

    gcc and g++分别是gnu的c & c++编译器. 从源代码到可执行文件的四步 gcc/g++在执行编译工作的时候,总共需要4步 1.预处理,生成.i的文件,用到预处理器cpp.这一步 ...