Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use FreeMarker in SpringBoot

Shulou Source: shulou.com Published: 2022-06-01 09:36:07 10月03日 Update

This article mainly introduces the relevant knowledge of "how to use 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 use FreeMarker in SpringBoot" can help you solve the problem.

FreeMarker introduction

Apache FreeMarker? Is a template engine: a Java library for generating text output from templates and change data (HTML web pages, email, configuration files, source code, etc.). Templates are written in FreeMarker template language (FTL), which is a simple specialized language (unlike a complete programming language like PHP). Typically, you use a common programming language, such as Java, to prepare data (publish database queries, do business calculations). Apache FreeMarker then uses a template to display the prepared data. In the template, you will focus on how to render the data, while outside the template, you will focus on the data to be rendered.

Thymeleaf is officially recommended by SpringBoot. Since I am relatively familiar with FreeMarker, I will first introduce how to use the current front-end page of FreeMarker in SpringBoot.

Steps for SpringBoot to use FreeMarker

The first step is in pom.xml? The specific code for introducing spring-boot-starter-freemarker dependency in is as follows:

Org.springframework.boot spring-boot-starter-freemarker

The second step is to create a test directory by templates under resources and create a new freemarkDemo.ftl content as follows:

${msg}

? The third step is to create a Controller that accesses the freemarkDemo.ftl.

@ Controller@RequestMapping ("/ hello") public class HelloWorldController {@ RequestMapping ("/ test") public String test (Model model) {model.addAttribute ("msg", "SpringBoot With Freemark helloworld!"); return "test/helloworld";}} Test

Enter the URL: http://localhost:8080/sbe/hello/test? of the Controller that accesses the FreeMarker page in the browser. Carry on the test, the test result is as follows:

This is the end of the introduction to "how to use 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: Template data language knowledge test content is in programming language industry page preparation programming different practical business code front end official practical practical Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn Xiaomi Docker Shulou Technology MariaDB