依赖
<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的详细内容。更多信息请关注PHP中文网其他相关文章!
声明
本文转载于:亿速云。如有侵权,请联系admin@php.cn删除

本文讨论了使用Maven和Gradle进行Java项目管理,构建自动化和依赖性解决方案,以比较其方法和优化策略。

本文使用Maven和Gradle之类的工具讨论了具有适当的版本控制和依赖关系管理的自定义Java库(JAR文件)的创建和使用。

本文讨论了使用咖啡因和Guava缓存在Java中实施多层缓存以提高应用程序性能。它涵盖设置,集成和绩效优势,以及配置和驱逐政策管理最佳PRA

本文讨论了使用JPA进行对象相关映射,并具有高级功能,例如缓存和懒惰加载。它涵盖了设置,实体映射和优化性能的最佳实践,同时突出潜在的陷阱。[159个字符]

Java的类上载涉及使用带有引导,扩展程序和应用程序类负载器的分层系统加载,链接和初始化类。父代授权模型确保首先加载核心类别,从而影响自定义类LOA


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章
R.E.P.O.能量晶体解释及其做什么(黄色晶体)
4 周前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳图形设置
4 周前By尊渡假赌尊渡假赌尊渡假赌
刺客信条阴影:贝壳谜语解决方案
2 周前ByDDD
R.E.P.O.如果您听不到任何人,如何修复音频
4 周前By尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解锁Myrise中的所有内容
1 个月前By尊渡假赌尊渡假赌尊渡假赌

热工具

适用于 Eclipse 的 SAP NetWeaver 服务器适配器
将Eclipse与SAP NetWeaver应用服务器集成。

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

Atom编辑器mac版下载
最流行的的开源编辑器

Dreamweaver CS6
视觉化网页开发工具

EditPlus 中文破解版
体积小,语法高亮,不支持代码提示功能