栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

springboot 整合 freemarker代码实例

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

springboot 整合 freemarker代码实例

这篇文章主要介绍了springboot 整合 freemarker代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

依赖


  org.springframework.boot
  spring-boot-starter-parent
  2.1.6.RELEASE



  
    org.springframework.boot
    spring-boot-starter-freemarker
  
  
    org.projectlombok
    lombok
  
  
    com.squareup.okhttp3
    okhttp
    3.9.1
  
  
    commons-io
    commons-io
    2.6
  

  
    org.springframework.boot
    spring-boot-starter-web
  
  
    org.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());
  }
}

模板文件





  
  


  
    <#if students??>
      <#list students as stu>
 
序号 姓名 年龄 金钱 出生日期
${stu_index} style="background-color: #108cee" >${stu.name} style="background-color: #108cee">${stu.age} ${stu.money} ${stu.birthday?date},${stu.birthday?time},${stu.birthday?string("yyyy年MM月dd日")}
姓名:${stuMap['stu1'].name} 年龄: ${stuMap.stu1.age} <#list stuMap?keys as k> 姓名: ${stuMap[k].name} 年龄: ${stuMap[k].age} ${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}//默认显示逗号分隔



  
    
    Title
  
  
    
      <#if model??>
 <#list model as item>
   
   
 
    
    
  
  

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 entity = restTemplate.getForEntity(dataUrl, Map.class);
    Map body = entity.getBody();
    map.putAll((Map) body);
    //    restTemplate.getForEntity("")
    return "index_banner";
  }

  @RequestMapping("/test2")
  public String test2(Map 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 students = new ArrayList<>();
    students.add(stu1);
    students.add(stu2);
    //模板使用的数据
    map.put("students", students);

    HashMap stuMap = new HashMap<>();
    stuMap.put("stu1", stu1);
    stuMap.put("stu2", stu2);
    map.put("stuMap", stuMap);
    //返回模板的位置,基于 resources/templates
    return "test2";
  }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/136758.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号