springboot 整合 freemarker代碼實(shí)例
這篇文章主要介紹了springboot 整合 freemarker代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
依賴(lài)
<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
server: port: 8001 spring: application: name: test-freemarker freemarker: cache: false settings: template_update_delay: 0 template-loader-path: classpath:/templates/
啟動(dòng)類(lèi)
@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>序號(hào)</td>
<td>姓名</td>
<td>年齡</td>
<td>金錢(qián)</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)!'中國(guó)建設(shè)銀行'}//默認(rèn)值方式處理空值
${students?size}//集合大小
<#assign text="{'bank':'中國(guó)農(nóng)業(yè)銀行', 'address':'北大街'}">
<#assign data=text?eval>
開(kāi)戶(hù)行: ${data.bank} 地址: ${data.address}
${123456123?c}//不顯示逗號(hào)分隔
${123456123}//默認(rèn)顯示逗號(hào)分隔
</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("孫權(quán)");
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);
//模板使用的數(shù)據(jù)
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";
}
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Docker環(huán)境下Spring Boot應(yīng)用內(nèi)存飆升分析與解決場(chǎng)景分析
當(dāng)運(yùn)行一個(gè)Spring Boot項(xiàng)目時(shí),如果未設(shè)置JVM內(nèi)存參數(shù),Spring Boot默認(rèn)會(huì)采用JVM自身默認(rèn)的配置策略,接下來(lái)通過(guò)本文給大家介紹Docker環(huán)境下Spring Boot應(yīng)用內(nèi)存飆升分析與解決方法,需要的朋友參考下吧2021-08-08
使用kafka-console-consumer.sh不停報(bào)WARN的問(wèn)題及解決
這篇文章主要介紹了使用kafka-console-consumer.sh不停報(bào)WARN的問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-03-03
Java多線程的原子性,可見(jiàn)性,有序性你都了解嗎
這篇文章主要為大家詳細(xì)介紹了Java多線程的原子性,可見(jiàn)性,有序性,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助2022-03-03
使用Zxing實(shí)現(xiàn)二維碼生成器內(nèi)嵌圖片
二維碼在現(xiàn)實(shí)中的應(yīng)用已經(jīng)很廣泛了,本文介紹了使用Zxing實(shí)現(xiàn)二維碼生成器內(nèi)嵌圖片,有需要的可以了解一下。2016-10-10
解析Java中的隊(duì)列和用LinkedList集合模擬隊(duì)列的方法
這篇文章主要介紹了解析Java中的隊(duì)列和用LinkedList集合模擬隊(duì)列的方法,相關(guān)算法的實(shí)現(xiàn)也是ACM中的常見(jiàn)題目,需要的朋友可以參考下2015-08-08
Java代碼實(shí)現(xiàn)矩形覆蓋實(shí)例
這篇文章主要介紹了Java代碼實(shí)現(xiàn)矩形覆蓋實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,,需要的朋友可以參考下2019-06-06
Java異常簡(jiǎn)介和架構(gòu)_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
這篇文章主要分享了Java異常簡(jiǎn)介和架構(gòu),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06
SpringBoot2.x漏洞將logback1.2.x 升級(jí)至1.3.x
安全部門(mén)在代碼漏洞掃描中發(fā)現(xiàn)logback 1.2.x版本存在CVE漏洞,建議升級(jí)至1.3.x版本,本文就來(lái)介紹了logback1.2.x 升級(jí)至1.3.x,具有一定的參考價(jià)值,感興趣的可以了解一下2024-09-09

