第一步:pom.xml引入各种依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0
<groupId>org.example
<artifactId>untitled5
<version>1.0-SNAPSHOT
<groupId>org.springframework.boot
<artifactId>spring-boot-starter-parent
<version>1.5.9.RELEASE
<!--springweb 核心组件-->
<groupId>org.springframework.boot
<artifactId>spring-boot-starter-web
<!-- 引入freeMarker的依赖包. -->
<groupId>org.springframework.boot
<artifactId>spring-boot-starter-freemarker
</project>
第二步:写个控制器,并返回些数据
@Controller
@RequestMapping("/stu")
public class StudentController {
@RequestMapping("/list")
public Stringlist(Model model){
model.addAttribute("username","ma");
model.addAttribute("age",25);
List list=new ArrayList();
list.add(new Student(1001,"王大锤","男"));
list.add(new Student(1002,"汪德发","女"));
list.add(new Student(1005,"西门吹雪","男"));
model.addAttribute("list",list);
return "stu/list";
}
}
第三步:创建.ftl模板文件
在src/main/resources/创建一个templates文件夹,后缀为*.ftl
<!DOCTYPE html>
<head lang="en">
<meta charset="UTF-8" />
欢迎:${username}
<#if age<18>小朋友
<#elseif age<30>先生
<#else >大叔
</#if>登录
<table border="1">
<td>ID
<td>姓名
<td>性别
<#list listas stu>
<td>${stu.id}
<td>${stu.name}
<td>${stu.gender}
</html>