SpringBoot模板
导入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
application.properties 的后缀改成了application.yml
Spring Boot官方文档建议在开发时将缓存关闭,那就在application.properties文件中加入下面这行
spring.thymeleaf.cache=false
application.yml
server:
port: 80
servlet:
context-path: /
spring:
thymeleaf:
cache: false
正式环境还是要将缓存开启的
Thymeleaf模板
关于Thymeleaf的优点,我只说一条:它就是html页面。下面直接上代码
后端代码
User
package com.dyz.springboot01.entity;
import lombok.Data;
@Data
public class User {
private Integer uid;
private String uname;
private Integer pwd;
public User(){
}
public User(Integer uid, String uname, Integer pwd) {
this.uid = uid;
this.uname = uname;
this.pwd = pwd;
}
}
UserController
package com.dyz.springboot01.controller;
import com.dyz.springboot01.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import java.util.ArrayList;
import java.util.List;
/**
* @author DZY
* @site www.1620797557.com
* @company xxx公司
* @create 2019-12-30 8:45
*
* 介绍thymeleaf模块引擎的应用
*/
@Controller
@RequestMapping("/thymeleaf")
public class UserController {
@RequestMapping("/list")
public ModelAndView list(){
ModelAndView m=new ModelAndView();
m.addObject("title","学生列表");
List<User> list=new ArrayList<>();
list.add(new User(1,"DZY",429));
list.add(new User(2,"ASD",978));
list.add(new User(3,"ZXC",865));
m.addObject("users",list);
// 为什么需要讲解 html 转义
m.addObject("msg","<span style='color:red'>优秀学生是小王</span>");
m.setViewName("list");
return m;
}
}
前端代码
list
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>thymeleaf知识点</title>
</head>
<body>
<table border="1" cellpadding="0" cellspacing="0" th:width="600px">
<caption th:text="${title}"></caption>
<tr>
<th>ID</th>
<th>名字</th>
<th>密码</th>
</tr>
<tr th:each="u : ${users}">
<td th:text="${u.uid}"></td>
<td th:text="${u.uname}"></td>
<td th:text="${u.pwd}"></td>
</tr>
</table>
<p th:utext="${msg}"></p>
</body>
</html>
Freemarker模板
学习网站了解一下 :http://freemarker.foofun.cn/
application.yml
spring:
thymeleaf:
cache: false
freemarker:
# 设置模板后缀名
suffix: .ftl
# 设置文档类型
content-type: text/html
# 设置页面编码格式
charset: UTF-8
# 设置页面缓存
cache: false
# 设置ftl文件路径,默认是/templates,为演示效果添加role
template-loader-path: classpath:/templates/role
mvc:
static-path-pattern: /static/**
创建freemarker这个类
前端代码
list.ftl
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>讲解freemarker知识</title>
</head>
<body>
<h2>取值</h2>
<h3>提供默认值</h3>
welcome 【${name!'未知'}】 to freemarker!
<h3>exists用在逻辑判断</h3>
<#if name?exists>
${name}
</#if>
<h2>条件</h2>
<#if sex=='girl'>
女
<#elseif sex=='boy'>
男
<#else>
保密
</#if>
<h2>循环</h2>
<table border="1px" width="600px">
<thead>
<tr>
<td>ID</td>
<td>角色名</td>
<td>描述</td>
</tr>
</thead>
<tbody>
<#list roles as role>
<tr>
<td>${role.rid}</td>
<td>${role.rname}</td>
<td>${role.rdemo}</td>
</tr>
</#list>
</tbody>
</table>
<h2>include</h2>
<#include 'foot.ftl'>
<h2>局部变量(assign)/全局变量(global)</h2>
<#assign ctx1>
${springMacroRequestContext.contextPath}
</#assign>
<#global ctx2>
${springMacroRequestContext.contextPath}
</#global>
${ctx1}和${ctx2}
</body>
</html>
foot.ftl
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>调用界面</title>
</head>
<body>
今天下雨了 记得保暖打伞
</body>
</html>
后端代码
Role
package com.dyz.springboot01.entity;
import lombok.Data;
@Data
public class Role {
private Integer rid;
private String rname;
private String rdemo;
public Role(){
}
public Role(Integer rid, String rname, String rdemo) {
this.rid = rid;
this.rname = rname;
this.rdemo = rdemo;
}
}
RoleController
package com.xwt.springboot1.controller;
import com.xwt.springboot1.entity.Role;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import java.util.ArrayList;
import java.util.List;
/**
* @author DZY
* @site www.1620797557.com
* @company xxx公司
* @create 2019-12-30 8:45
* 介绍freemarker模块引擎的应用
*/
@Controller
@RequestMapping("/freemarker")
public class RoleController {
@RequestMapping("/list")
public ModelAndView list(){
ModelAndView m=new ModelAndView();
m.setViewName("list");
m.addObject("name","学生列表");
m.addObject("sex","boy");
List<Role> roles=new ArrayList<>();
roles.add(new Role(1,"网管","网吧总管"));
roles.add(new Role(2,"包租婆","管水管电管房租"));
roles.add(new Role(3,"总管","公司老总"));
m.addObject("roles",roles);
return m;
}
}