freemarker from entry to the master

table of Contents

 

I. Overview

Two: Freemarker of Helloworld

Three: freemarker template syntax

1. Access the map of the key

2. Access the POJO properties

3. Take the data set

 4. Analyzing

5. Date

Processing 6.Null value

7.include

Four: Springboot integration freemarker


I. Overview

If you use a web application and jsp as template data show, when this application is running, the corresponding jsp page will be translated into the corresponding java file, which is the servlet. With respect to the terms of html, html static pages that do not have direct access to translation speed quickly. How to solve the problem jsp compiled into a java file and then respond?

FreeMarker is a written in the Java language template engine, 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 used to achieve technical presentation layer, but also can be used to generate such as XML, JSP or Java. Currently enterprises: mainly to do with Freemarker static page or page impressions,

principle:

Two: Freemarker of Helloworld

Create a new web project, the complete project directory structure is as follows

Use freemarker need to introduce jar package

<dependency>

  <groupId>org.freemarker</groupId>

  <artifactId>freemarker</artifactId>

  <version>2.3.23</version>

</dependency>

First create a template file ftl

New ftls file in the webapp directory folder (name and location of any storage) to store the template Freemarker

Hello.ftl create a template file, (freemarker to expand the name is not required. Ftl often used as the extension), enter the following below the contents to be replaced

<html>

<head>

   <meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/>

</head>

<body>

测试:${hello}

</body>

</html>

 Wherein the grammar is $ {hello} Freemarker the label. This tag will be replaced Freemarker specified in the program content.

testing method

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

Three: freemarker template syntax

1. Access the map of the key

${key}

2. Access the POJO properties

${stu.id}-

${stu.name}

Stu pojo which is the object corresponding key: This object is passed in the past on map

3. Take the data set

Recycling format :

<#list be recycled data as data cycles >

</#list>

Examples are as follows

Taking cycle index

<#list studentList as student>

       ${student_index}

</#list>

 4. Analyzing

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

Examples

5. Date

${date?date} 2016-9-13

${date?time} 17:53:55

${date?datetime} 2016-9-13 17:53:55

${date?string("yyyy/MM/dd")}

date is the attribute name

dataModel.put("date",new Date());

Processing 6.Null value

If the template referenced key does not exist, an exception will be reported, with $ {key!} Represents the empty string

处理: ${aaa!"默认值"}或者${aaa!}代表空字符串

7.include

Ftl a template can reference another template

<#include "template name" >

Such as:

<#include "hello.ftl">

Four: Springboot integration freemarker

See

https://www.cnblogs.com/javayihao/p/11881033.html

 

                         No. One more public java java actual project data, technical work. More importantly, the little ape is willing to be a friend you programmed the road!

Articles starting address: www.javayihao.top

No public debut: java One

 

HTTPS: // on ohh .cn blog .com / Java One / fear /11881033.HTML
[Http: // WU WU wū.Cn blog shàng.Com / Java Yi Hao / PA / 11881033.HTML]
HTTPS: // on ohh .cn blog .com / Java One / fear /11881033.HTML

Guess you like

Origin www.cnblogs.com/javayihao/p/11881053.html