这篇文章主要介绍了springboot 整合 freemarker代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
依赖
org.springframework.boot spring-boot-starter-parent2.1.6.RELEASE org.springframework.boot spring-boot-starter-freemarkerorg.projectlombok lombokcom.squareup.okhttp3 okhttp3.9.1 commons-io commons-io2.6 org.springframework.boot spring-boot-starter-weborg.springframework.boot spring-boot-starter-test
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());
}
}
模板文件
| 序号 | 姓名 | 年龄 | 金钱 | 出生日期 |
| ${stu_index} | style="background-color: #108cee"#if> >${stu.name} | style="background-color: #108cee"#if>>${stu.age} | ${stu.money} | ${stu.birthday?date},${stu.birthday?time},${stu.birthday?string("yyyy年MM月dd日")} |
Title
<#if model??>
<#list model as item>
#list>
#if>
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 map){
String dataUrl = "http://localhost:31001/cms/config/getmodel/5a791725dd573c3574ee333f";
ResponseEntity
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。



