0

0

springboot中如何整合freemarker

王林

王林

发布时间:2023-05-17 18:07:06

|

891人浏览过

|

来源于亿速云

转载

依赖


  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/

启动类

Shopxp网上购物系统
Shopxp网上购物系统

Shopxp购物系统历经多年的考验,并在推出shopxp免费购物系统下载之后,收到用户反馈的各种安全、漏洞、BUG、使用问题进行多次修补,已经从成熟迈向经典,再好的系统也会有问题,在完善的系统也从在安全漏洞,该系统完全开源可编辑,当您下载这套商城系统之后,可以结合自身的技术情况,进行开发完善,当然您如果有更好的建议可从官方网站提交给我们。Shopxp网上购物系统完整可用,无任何收费项目。该系统经过

下载
@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} >${stu.name} >${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
  
  
    
    

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";
  }
}

相关专题

更多
C++ 高级模板编程与元编程
C++ 高级模板编程与元编程

本专题深入讲解 C++ 中的高级模板编程与元编程技术,涵盖模板特化、SFINAE、模板递归、类型萃取、编译时常量与计算、C++17 的折叠表达式与变长模板参数等。通过多个实际示例,帮助开发者掌握 如何利用 C++ 模板机制编写高效、可扩展的通用代码,并提升代码的灵活性与性能。

10

2026.01.23

php远程文件教程合集
php远程文件教程合集

本专题整合了php远程文件相关教程,阅读专题下面的文章了解更多详细内容。

29

2026.01.22

PHP后端开发相关内容汇总
PHP后端开发相关内容汇总

本专题整合了PHP后端开发相关内容,阅读专题下面的文章了解更多详细内容。

21

2026.01.22

php会话教程合集
php会话教程合集

本专题整合了php会话教程相关合集,阅读专题下面的文章了解更多详细内容。

21

2026.01.22

宝塔PHP8.4相关教程汇总
宝塔PHP8.4相关教程汇总

本专题整合了宝塔PHP8.4相关教程,阅读专题下面的文章了解更多详细内容。

13

2026.01.22

PHP特殊符号教程合集
PHP特殊符号教程合集

本专题整合了PHP特殊符号相关处理方法,阅读专题下面的文章了解更多详细内容。

11

2026.01.22

PHP探针相关教程合集
PHP探针相关教程合集

本专题整合了PHP探针相关教程,阅读专题下面的文章了解更多详细内容。

8

2026.01.22

菜鸟裹裹入口以及教程汇总
菜鸟裹裹入口以及教程汇总

本专题整合了菜鸟裹裹入口地址及教程分享,阅读专题下面的文章了解更多详细内容。

55

2026.01.22

Golang 性能分析与pprof调优实战
Golang 性能分析与pprof调优实战

本专题系统讲解 Golang 应用的性能分析与调优方法,重点覆盖 pprof 的使用方式,包括 CPU、内存、阻塞与 goroutine 分析,火焰图解读,常见性能瓶颈定位思路,以及在真实项目中进行针对性优化的实践技巧。通过案例讲解,帮助开发者掌握 用数据驱动的方式持续提升 Go 程序性能与稳定性。

9

2026.01.22

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
Redis6入门到精通超详细教程
Redis6入门到精通超详细教程

共47课时 | 5.3万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号