table of Contents
Three: freemarker template syntax
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
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
Wherein the grammar is $ {hello} Freemarker the label. This tag will be replaced Freemarker specified in the program content.
testing method
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
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