FreeMarker 入门教程
欢迎来到 FreeMarker 入门教程
随便写的,有什么不足请留言指教
项目运行截图
项目url:http://localhost:8080/user
git 下载地址
项目链接:https://github.com/oumingyuan/freemarker.git
软件环境
运行我的项目,你需要如下的环境
名称 | 版本 |
---|---|
java环境 | JDK 8 |
集成开发环境 | IDEA |
打包工具 | gradle |
版本工具 | git |
项目重要文件的目录结构
重要的文件是用红色字体标注的
- FreeMaker
- src
- main
- java
- com.example.freemarker
- controller
- UserController.java
- entity
- User.java
- controller
- com.example.freemarker
- resource
- templates
- user
- list.ftl
- user
- application.yml
- templates
- java
- build.gradle
- main
- src
UserController.java
package com.example.freemarker.controller;
import java.util.LinkedList;
import java.util.List;
import com.example.freemarker.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class UserController {
@RequestMapping("user")
public String user(Model m) {
List<User> list = new LinkedList<>();
User u1 = new User(1, "hello1", "13336980260");
User u2 = new User(2, "hello2", "13336980260");
User u3 = new User(3, "hello3", "13336980260");
list.add(u1);
list.add(u2);
list.add(u3);
m.addAttribute("userList", list);
m.addAttribute("listName", "user list");
return "user/list";
}
}
User.java
package com.example.freemarker.entity;
public class User {
private long id;
private String name;
private String phone;
public User(long id, String name, String phone) {
this.id = id;
this.name = name;
this.phone = phone;
}
public long getId() {
return id;
}
public String getName() {
return name;
}
public String getPhone() {
return phone;
}
}
list.ftl
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"
xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3" lang="">
<head>
<meta content="text/html;charset=utf-8"/>
<title>freemarker</title>
<link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/4.1.0/css/bootstrap.min.css">
<script src="https://cdn.staticfile.org/jquery/3.2.1/jquery.min.js"></script>
<script src="https://cdn.staticfile.org/popper.js/1.12.5/umd/popper.min.js"></script>
<script src="https://cdn.staticfile.org/twitter-bootstrap/4.1.0/js/bootstrap.min.js"></script>
<style>
h2 {
text-align: center;
margin-top: 50px;
margin-bottom: 30px;
}
</style>
</head>
<body>
<div class="container">
<h2>${listName}</h2>
<table class="table table-hover">
<thead>
<tr>
<th>id</th>
<th>name</th>
<th>phone</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>LiLei</td>
<td>13336980260</td>
</tr>
<tr>
<td>2</td>
<td>HanMei</td>
<td>15609682037</td>
</tr>
<#list userList as user>
<tr>
<td>${user.id}</td>
<td>${user.name}</td>
<td>${user.phone}</td>
</tr>
</#list>
</tbody>
</table>
</div>
</body>
</html>
application.yml
spring:
freemarker:
allow-request-override: false
cache: false
check-template-location: true
charset: utf-8
content-type: text/html
expose-request-attributes: false
expose-session-attributes: false
expose-spring-macro-helpers: false
suffix: .ftl
template-loader-path: classpath:/templates/
build.gradle
plugins {
id 'org.springframework.boot' version '2.1.4.RELEASE'
id 'java'
}
apply plugin: 'io.spring.dependency-management'
group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-freemarker'
implementation 'org.springframework.boot:spring-boot-starter-web'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}