Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The principle and Application of FreeMarker

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "the principle and application of FreeMarker". In the daily operation, I believe that many people have doubts about the principle and application of FreeMarker. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts about the principle and application of FreeMarker! Next, please follow the editor to study!

FreeMarker is a template engine developed in Java, that is, a general tool based on templates and data to be changed, and used to generate output text (HTML web pages, email, configuration files, source code, etc.). It is not for end users, but a java class library, a component that programmers can embed in the products they develop.

FreeMarker does not care about the source of the data, but displays the data template in the template and outputs the file (usually html, but can also generate text files in other formats) according to the content of the template.

Data model: data model can be a basic type or a complex type such as List,Map,Pojo in java

Getting started:

Add dependencies

Org.springframework.boot spring-boot-starter-freemarker org.springframework.boot spring-boot-starter-test org.springframework.boot spring-boot-starter-web org.projectlombok lombok 1.18.8 org.apache.commons commons-io 1.3.2 com.squareup.okhttp3 okhttp 4.1.0

Second, write startup classes

@ SpringBootApplicationpublic class FreemarkerApplication {public static void main (String [] args) {SpringApplication.run (FreemarkerApplication.class);}}

Third, write entity classes

@ Data@ToString@AllArgsConstructor@NoArgsConstructorpublic class Student {private String name; private int age; private Date birthday; private Float money; private List friends; private Student bestFriends;}

Create a templates folder and a template file test1.ftl under resources

Titlehello ${name}!

5. Write controller

@ Controller@RequestMapping ("/ freemarker") public class FreemarkerController {@ RequestMapping ("/ test1") public String freemarker (Map map) {map.put ("name", "cxy"); return "test1";}}

VI. Start the test

Freemarker fundamentals:

1. Comments, that is, the content in between will be ignored by freemarker

2. Interpolation, that is, ${}, will be replaced by freemarker with a real value

3. FTL, similar to the html tag, is distinguished by a # before the name, and freemarker parses the expression or logic in the tag.

4. Text, only text information, output content directly

List instruction: traversing data in a template

${stu.index} ${stu.name} ${stu.age} ${stu.money}

Map instruction

${k.index-1} ${stumap [k] .name} ${stumap [k] .age} ${stumap [k] .money}

If instruction

${k.index-1}

Null handling: to determine whether a variable is in use?

${k.index-1} ${stumap [k] .name} ${stumap [k] .age} ${stumap [k] .money}

Note: missing variable defaults are used! Specify a default value, which is displayed when the variable is empty.

Built-in function

1. Collection size: ${collection name? size}

2. Date formatting:

${today?date}, showing the year, month and day

${today?time}, showing hours, minutes and seconds

${today?datetime}, showing the date plus time

${today?string (yyyy-MM-dd)}, displayed in character format

3. ${money?c} display the number as a string, otherwise add a comma every three digits

4. Convert a string to an object

Static implementation

1. Use template files to statically

2. Statically use template strings

Template static test code

Public class FreemarkerTest {@ Test public void testGenerateHtmk () throws IOException, TemplateException {Configuration configuration = new Configuration (Configuration.getVersion ()); URL resource = this.getClass (). GetClassLoader () .getResource ("templates/"); String path = resource.getPath (); configuration.setDirectoryForTemplateLoading (new File (path)); configuration.setDefaultEncoding ("utf-8"); Template template = configuration.getTemplate ("test1.ftl") System.out.println (template); Map map = new HashMap (); map.put ("name", "dark horse programmer"); String content = FreeMarkerTemplateUtils.processTemplateIntoString (template, map); System.out.println (content);}} at this point, the study on "the principles and applications of FreeMarker" is over, hoping to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report