天天看点

freemarker ftl模板_SpringBoot整合FreeMarker

freemarker ftl模板_SpringBoot整合FreeMarker

1.导入依赖

<

dependency

>

<

groupId

>org.springframework.boot</

groupId

>

<

artifactId

>spring-boot-starter-freemarker</

artifactId

>

</

dependency

>

2.在application.properties文件添加配置

########## 配置freemarker ###########是否开启缓存

spring.freemarker.cache

=

false

#路径

spring.freemarker.template-loader-path

=

classpath:/templates

#文件后缀

spring.freemarker.suffix

=

.ftl spring.freemarker.charset

=

UTF-8 spring.freemarker.content-type

=

text/html

3.创建controller

package

com.qf.controller;

import

org.springframework.stereotype.Controller;

import

org.springframework.ui.Model;

import

org.springframework.web.bind.annotation.RequestMapping;

@Controller

@RequestMapping(

"/freemarker"

)

public class

FreeMarkerController {

@RequestMapping(

"/hello"

)

public

String hello(Model model){

System.

out

.println(

"hello freemarker"

);

model.addAttribute(

"hello"

,

"hello freemarker !"

);

return "/freemarker"

;

}

}

4.在templates目录下创建freemarker.ftl

<!DOCTYPE

html

>

<

html

>

<

head

>

<

meta charset="UTF-8"

>

<

title

>Title</

title

>

</

head

>

<

body

>

hello.ftl

<

hr

>

${hello}

</

body

>

</

html

>

5.访问controller测试即可

补充:FreeMarker生成页面

1.在D盘ftl文件夹下编写hello.ftl和student.ftl文件

hello.ftl

<html>

<head>

<title>student</title>

</head>

<body>

${hello}

</body>

</html>

student.ftl

<html>

<head>

<title>student</title>

</head>

<body>

<table width=600>

<tr>

<th>index</th>

<th>id</th>

<th>name</th>

<th>age</th>

<th>address</th>

</tr>

<#list stuList as stu>

<#if stu_index % 2 == 0>

<tr bgcolor="red">

<#else>

<tr bgcolor="green">

</#if>

<td>${stu_index}</td>

<td>${stu.id}</td>

<td>${stu.name}</td>

<td>${stu.age}</td>

<td>${stu.address}</td>

</tr>

</#list>

</table>

</body>

</html>

2.创建实体类

package

com.qf.entity;

public class

Student {

private

Integer

id

;

private

String

name

;

private

Integer

age

;

private

String

address

;

public

Student(Integer id, String name, Integer age, String address) {

this

.

id

= id;

this

.

name

= name;

this

.

age

= age;

this

.

address

= address;

}

public

Student() {

}

public

Integer getId() {

return id

;

}

public void

setId(Integer id) {

this

.

id

= id;

}

public

String getName() {

return name

;

}

public void

setName(String name) {

this

.

name

= name;

}

public

Integer getAge() {

return age

;

}

public void

setAge(Integer age) {

this

.

age

= age;

}

public

String getAddress() {

return address

;

}

public void

setAddress(String address) {

this

.

address

= address;

}

}

3.创建测试类并运行

package

com.qf.test;

import

com.qf.entity.Student;

import

freemarker.template.Configuration;

import

freemarker.template.Template;

import

org.junit.Test;

import

java.io.File;

import

java.io.FileWriter;

import

java.util.ArrayList;

import

java.util.HashMap;

public class

FreeMarkerTest {

@Test

public void

test_freemarker()

throws

Exception{

//获取配置对象

Configuration configuration =

new

Configuration(Configuration.getVersion());

//设置字符集

configuration.setDefaultEncoding(

"utf-8"

);//设置加载的模版目录

configuration.setDirectoryForTemplateLoading(

new

File(

"D:/ftl"

));

//使用map集合加载数据

HashMap<String, String> map =

new

HashMap<>();

map.put(

"hello"

,

"hello freemarker"

);//创建输出流对象

FileWriter fileWriter =

new

FileWriter(

new

File(

"D:/ftl_html/hello.html"

));//获取加载的模板

Template template = configuration.getTemplate(

"hello.ftl"

);

//生成html文件

template.process(map,fileWriter);

//关流

fileWriter.close();

}

@Test

public void

test_freemarker_student()

throws

Exception{

//获取配置对象

Configuration configuration =

new

Configuration(Configuration.getVersion());//设置字符集

configuration.setDefaultEncoding(

"utf-8"

);

//设置加载的模版目录

configuration.setDirectoryForTemplateLoading(

new

File(

"D:/ftl"

));

//创建List集合获取多个元素

ArrayList<Student> students =

new

ArrayList<>();

students.add(

new

Student(1001,

"jack"

,18,

"郑州二七"

));

students.add(

new

Student(1002,

"rose"

,19,

"郑州中原"

));

students.add(

new

Student(1003,

"tom"

,20,

"郑州金水"

));//使用map集合加载数据

HashMap<String,ArrayList> map =

new

HashMap<>();

map.put(

"stuList"

,students);

//创建输出流对象

FileWriter fileWriter =

new

FileWriter(

new

File(

"D:/ftl_html/student.html"

));//获取加载的模板

Template template = configuration.getTemplate(

"student.ftl"

);

//生成html文件

template.process(map,fileWriter);

//关流

fileWriter.close();

}

}

继续阅读