您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關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 == '劉備')></#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)!'中國建設銀行'}//默認值方式處理空值 ${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" ></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就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。