Dependency
<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 parameter path
server: port: 8001 spring: application: name: test-freemarker freemarker: cache: false settings: template_update_delay: 0 template-loader-path: classpath:/templates/
Startup class
@SpringBootApplication public class FreemarkerApplication { public static void main(String[] args) { SpringApplication.run(FreemarkerApplication.class, args); } @Bean public RestTemplate restTemplate(){ return new RestTemplate(new OkHttp3ClientHttpRequestFactory()); } }
Template file
nbsp;html> <!-- resources/templates/test2.ftl --> <meta> <title></title>
序号 | 姓名 | 年龄 | 金钱 | 出生日期 |
${stu_index} | #if> >${stu.name} | #if>>${stu.age} | ${stu.money} | ${stu.birthday?date},${stu.birthday?time},${stu.birthday?string("yyyy年MM月dd日")} |
nbsp;html> <!-- resources/templates/index_banner.ftl --> <meta> <title>Title</title> <div> <div></div> #list> #if> </div> <script> //... </script>
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> 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> 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> stuMap = new HashMap(); stuMap.put("stu1", stu1); stuMap.put("stu2", stu2); map.put("stuMap", stuMap); //返回模板的位置,基于 resources/templates return "test2"; } }</string></student></string></map></string>
The above is the detailed content of How to integrate freemarker in springboot. For more information, please follow other related articles on the PHP Chinese website!

The article discusses using Maven and Gradle for Java project management, build automation, and dependency resolution, comparing their approaches and optimization strategies.

The article discusses creating and using custom Java libraries (JAR files) with proper versioning and dependency management, using tools like Maven and Gradle.

The article discusses implementing multi-level caching in Java using Caffeine and Guava Cache to enhance application performance. It covers setup, integration, and performance benefits, along with configuration and eviction policy management best pra

The article discusses using JPA for object-relational mapping with advanced features like caching and lazy loading. It covers setup, entity mapping, and best practices for optimizing performance while highlighting potential pitfalls.[159 characters]

Java's classloading involves loading, linking, and initializing classes using a hierarchical system with Bootstrap, Extension, and Application classloaders. The parent delegation model ensures core classes are loaded first, affecting custom class loa


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Chinese version
Chinese version, very easy to use

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version
Useful JavaScript development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment