這篇文章主要介紹“springboot中如何整合freemarker”的相關(guān)知識(shí),小編通過(guò)實(shí)際案例向大家展示操作過(guò)程,操作方法簡(jiǎn)單快捷,實(shí)用性強(qiáng),希望這篇“springboot中如何整合freemarker”文章能幫助大家解決問(wèn)題。

為長(zhǎng)陽(yáng)等地區(qū)用戶提供了全套網(wǎng)頁(yè)設(shè)計(jì)制作服務(wù),及長(zhǎng)陽(yáng)網(wǎng)站建設(shè)行業(yè)解決方案。主營(yíng)業(yè)務(wù)為成都做網(wǎng)站、成都網(wǎng)站制作、長(zhǎng)陽(yáng)網(wǎng)站設(shè)計(jì),以傳統(tǒng)方式定制建設(shè)網(wǎng)站,并提供域名空間備案等一條龍服務(wù),秉承以專(zhuān)業(yè)、用心的態(tài)度為用戶提供真誠(chéng)的服務(wù)。我們深信只要達(dá)到每一位用戶的要求,就會(huì)得到認(rèn)可,從而選擇與我們長(zhǎng)期合作。這樣,我們也可以走得更遠(yuǎn)!
依賴
<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 參數(shù)路徑
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 == '劉備')></#if> >${stu.name}</td>
<td <#if (stu.age < 20)></#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)戶行: ${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" ></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";
}
}關(guān)于“springboot中如何整合freemarker”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí),可以關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,小編每天都會(huì)為大家更新不同的知識(shí)點(diǎn)。
分享文章:springboot中如何整合freemarker
網(wǎng)頁(yè)路徑:http://chinadenli.net/article16/jigcdg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供動(dòng)態(tài)網(wǎng)站、搜索引擎優(yōu)化、App設(shè)計(jì)、Google、域名注冊(cè)、網(wǎng)站排名
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)