With the rapid development of the Internet, the development of Web applications has become more and more common. To make web applications more readable and maintainable, developers often use web template engines for view rendering. In Java development, there are many popular web template engines, and FreeMarker is one of them.
This article will introduce the FreeMarker Web template engine and its use in Java API development, including its core features, configuration and its application in actual combat.
1. What is FreeMarker
FreeMarker is an open source Java template engine that uses a template-based method to generate static text or dynamic web pages. Its feature is the separation of templates and program code, which clarifies the boundaries between performance and logic, separates page rendering and business logic, and improves the readability and maintainability of the code. FreeMarker supports multiple template types such as text templates, XML templates, HTML templates, JSP tag libraries, etc., and can be integrated with a variety of web frameworks, such as Struts2, Spring MVC, etc.
FreeMarker has the following features:
2. Use of FreeMarker API
FreeMarker provides many APIs to use it to generate templates. Our initial call involves configuring FreeMarker to emit templates. Next, we show how to set up and use the FreeMarker API.
First, we need to add FreeMarker’s dependency in the project’s pom.xml file:
<dependency> <groupId>org.freemarker</groupId> <artifactId>freemarker</artifactId> <version>2.3.31</version> </dependency>
To create FreeMarker or you need a configuration to tell it how to load the template, refer to the following code example:
Configuration configuration = new Configuration(Configuration.VERSION_2_3_28); configuration.setClassForTemplateLoading(YourClass.class, "templates");
Among them, VERSION_2_3_28 is the FreeMarker version number, setClassForTemplateLoading() Method sets the path for FreeMarker to load templates.
Next, you need to set the input data of the template. In FreeMarker, this background is a Map, and this Map needs to contain all the data we want to use in the template. We can use the SimpleHash type to create this Map:
Map<String, Object> input = new HashMap<String, Object>(); input.put("title", "FreeMarker Example");
In this example, we added "title" as the key and "FreeMarker Example" as the value to the input.
Finally, we need to load the template and render the input data into the template, refer to the following code example:
Template template = configuration.getTemplate("example.ftl"); Writer out = new OutputStreamWriter(System.out); template.process(input, out); out.flush();
In this example, "example.ftl" is a template file. We use the configuration.getTemplate() method to load it, and the template.process() method to render the data in the input into the template, and finally output it through out.
FreeMarker template syntax defines template tags, built-in formats, and methods. Template markers are directives in templates, consisting of FreeMarker template code in a pair of ${} or 72637aecae1027e7d023ac098a170986 tags.
The following are some FreeMarker markers:
3. Application examples of FreeMarker
Below we will demonstrate how to use FreeMarker to write templates in Java API development.
First, we need to write a FreeMarker template file, for example, test.ftl:
<html> <head> <title>${title}</title> </head> <body> <h1>${title}</h1> <ul> <#list users as user> <li>${user.name} (${user.email})</li> </#list> </ul> </body> </html>
In this example, we Use the ${...} tag to reference the data in the input, and use the 364e9d80cdc5c997ad29773a08b6c2809fc13d131a1403f8a03377ed174a3a41 tag to loop through users and get the name and email attributes from each user.
Then, we need to set the data model, refer to the following code example:
Map<String, Object> input = new HashMap<String, Object>(); input.put("title", "FreeMarker Example"); ListuserList = new ArrayList (); userList.add(new User("Tom", "tom@example.com")); userList.add(new User("Jerry", "jerry@example.com")); input.put("users", userList); Configuration configuration = new Configuration(Configuration.VERSION_2_3_28); configuration.setClassForTemplateLoading(YourClass.class, "/templates"); Template template = configuration.getTemplate("test.ftl"); Writer out = new OutputStreamWriter(System.out); template.process(input, out); out.flush();
In this example, we A JavaBean class named User is created. When creating the Map, we use userList as the key and the List reference as the value, and add it to the input.
3. Summary
This article introduces the FreeMarker Web template engine and its use in Java API development. FreeMarker makes web application development easier while improving code readability and maintainability. By explaining the core features, configuration and application of FreeMarker in practice, we hope to help readers better understand and apply FreeMarker.
The above is the detailed content of Using FreeMarker for Web template engine processing in Java API development. For more information, please follow other related articles on the PHP Chinese website!