Get the App
SLTechnology News&Howtos  ›  Development  › 

How to integrate freemarker in springboot

Shulou Source: shulou.com Published: 2022-06-01 01:05:56 10月04日 Update

This article mainly introduces the relevant knowledge of "how to integrate freemarker in springboot". The editor shows you the operation process through an actual case. The operation method is simple, fast and practical. I hope this article "how to integrate freemarker in springboot" can help you solve the problem.

Dependence

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 parameter path

Server: port: 8001spring: application: name: test-freemarker freemarker: cache: false settings: template_update_delay: 0 template-loader-path: classpath:/templates/

Startup class

@ SpringBootApplicationpublic class FreemarkerApplication {public static void main (String [] args) {SpringApplication.run (FreemarkerApplication.class, args);} @ Bean public RestTemplate restTemplate () {return new RestTemplate (new OkHttp3ClientHttpRequestFactory ());}}

Template file

Serial number, name, age, money date of birth ${stu_index} ${stu.name} ${stu.age} ${stu.money} ${stu.birthday?date}, ${stu.birthday?time} ${stu.birthday?string ("yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy And use the? C output string ${students???c} ${(mozq.bank.address)!' China Construction Bank'} / / default values deal with null values ${students?size} / / Collection size bank of deposit: ${data.bank} address: ${data.address} ${123456123roomc} / / do not display comma separated ${123456123} / / default display comma separated 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 / / be careful not to use @ 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 (Liu Bei); stu1.setAge (18); stu1.setBirthday (new Date ()); stu1.setMoney (22225.8F); Student stu2 = new Student (); stu2.setName (Sun Quan); stu2.setAge (20); stu2.setBirthday (new Date ()); stu2.setMoney (24525.8F); stu2.setBestFriend (stu1); List students = new ArrayList (); students.add (stu1) Students.add (stu2); / / data used by the template map.put ("students", students); HashMap stuMap = new HashMap (); stuMap.put ("stu1", stu1); stuMap.put ("stu2", stu2); map.put ("stuMap", stuMap); / / returns the location of the template, based on resources/templates return "test2" }} this is the end of the introduction to "how to integrate freemarker in springboot". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

Tags: Integration name age template knowledge industry comma different practical location content parameters address size character string practicality actual serial number bank of deposit Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB Microsoft Shulou Technology MySQL Shulou Information