Freemarker achieve static pages (a)

1. static pages

Freemarker can be used to achieve static pages.

1.1 What is freemarker

FreeMarker is a Java language oftemplateEngine, which is based on a template to generate text output. FreeMarker has nothing to do with the Web container, that is in the Web, it does not know Servlet or HTTP. It not only can be usedPresentation layerImplementation technology, but also it can be used to generateXMLJSPorJavaWait.

Currently the enterprise: the main job of static pages or page impressions with Freemarker

1.2 Freemarker to use

The freemarker jar package added to the project.
Add rely on Maven project

<dependency>
  <groupId>org.freemarker</groupId>
  <artifactId>freemarker</artifactId>
  <version>2.3.23</version>
</dependency>

The basic idea Summary:

Use steps:
1. Set up the configuration file.
2. Create and load the template.
3. Obtain data.
4. Add data to the template.
5. Output page of the document.
6. Release resources.

Template:
$ {} the Test

@Test
   public void genFile() throws Exception {
   	// 第一步:创建一个Configuration对象,直接new一个对象。构造方法的参数就是freemarker对应的版本号。
   	Configuration configuration = new Configuration(Configuration.getVersion());
   	// 第二步:设置模板文件所在的路径。
   	configuration.setDirectoryForTemplateLoading(new File("E:/workspace10/e3-item-web/src/main/webapp/WEB-INF/ftl"));
   	// 第三步:设置模板文件使用的字符集。一般就是utf-8.
   	configuration.setDefaultEncoding("utf-8");
   	// 第四步:加载一个模板,创建一个模板对象。
   	Template template = configuration.getTemplate("test.ftl");
   	// 第五步:创建一个模板使用的数据集,可以是pojo也可以是map。一般是Map。
   	Map dataModel = new HashMap<>();
   	//向数据集中添加数据
   	dataModel.put("test", "this is my first freemarker test.");
   	// 第六步:创建一个Writer对象,一般创建一FileWriter对象,指定生成的文件名。
   	Writer out = new FileWriter(new File("E:/temp/out/test.html"));
   	// 第七步:调用模板对象的process方法输出文件。
   	template.process(dataModel, out);
   	// 第八步:关闭流。
   	out.close();
   }

1.3. The syntax template

1.3.1. The key access map

${key}

1.3.2. Access pojo of property

Student object. Student ID, name, age (you need to create a Student class pojo)

${key.property}
Student need to create a class of pojo

1.3.3 Take the data set

<#list studentList as student>
${student.id}/${studnet.name}
</#list>

Here Insert Picture DescriptionHere Insert Picture Description

1.3.4 taking cycle index

<#list studentList as student>
	${student_index}
</#list>

Here Insert Picture Description

1.3.5. Judge

<#if student_index % 2 == 0>
<#else>
</#if>

Here Insert Picture Description
The results are as follows:
Here Insert Picture Description

1.3.6. Date type format

Here Insert Picture Description

1.3.7 Processing Null value

Here Insert Picture Description

1.3.8. Include labels

<#include “模板名称”>

Here Insert Picture Description

1.4. Freemarker整合spring

Introducing jar package:
Freemarker jar package
Here Insert Picture Description

1.4.1. Creating integrated spring configuration file

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
        http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

	<bean id="freemarkerConfig"
		class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
		<property name="templateLoaderPath" value="/WEB-INF/ftl/" />
		<property name="defaultEncoding" value="UTF-8" />
	</bean>
</beans>

You need to write a test Controller

1.4.2. Controller

Request url: / genhtml
Parameters: None
Return Value: ok (String, required @ResponseBody)
business logic:
1, the spring FreeMarkerConfigurer object from the container.
2, the object is obtained from the Configuration FreeMarkerConfigurer object.
3, using the Configuration object gains Template object.
4, create a data set
5, the output file is created Writer object.
6, process method call template objects, generate the file.
7, the stream is closed.

Loading a configuration file:

@Controller
public class HtmlGenController {
   
   @Autowired
   private FreeMarkerConfigurer freeMarkerConfigurer;

   @RequestMapping("/genhtml")
   @ResponseBody
   public String genHtml()throws Exception {
   	// 1、从spring容器中获得FreeMarkerConfigurer对象。
   	// 2、从FreeMarkerConfigurer对象中获得Configuration对象。
   	Configuration configuration = freeMarkerConfigurer.getConfiguration();
   	// 3、使用Configuration对象获得Template对象。
   	Template template = configuration.getTemplate("test.ftl");
   	// 4、创建数据集
   	Map dataModel = new HashMap<>();
   	dataModel.put("test", "1000");
   	// 5、创建输出文件的Writer对象。
   	Writer out = new FileWriter(new File("E:/temp/term197/out/spring-freemarker.html"));
   	// 6、调用模板对象的process方法,生成文件。
   	template.process(dataModel, out);
   	// 7、关闭流。
   	out.close();
   	return "OK";
   }
}
Published 15 original articles · won praise 0 · Views 492

Guess you like

Origin blog.csdn.net/qq_36335126/article/details/103881330
Recommended