这篇文章主要介绍了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>