Thymeleaf 模板
thymeleaf优点:
1.静态html嵌入标签属性,浏览器可以直接打开模板文件,便于前后端联调。
2.springboot官方推荐方案。
导入pom 依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
Spring Boot官方文档建议在开发时将缓存关闭,那就在application.yml(application.properties)文件中加入下面这行
spring:
thymeleaf:
cache: false
后台代码
user
package com.lg.springboot01.entity;
import lombok.Data;
/**
* @author 废废
* @site
* @company xxx公司
* @create 2019-12-27 20:29
*/
@Data
public class User {
private String uid;
private String uname;
private String pwd;
public User(String uid, String uname, String pwd) {
this.uid = uid;
this.uname = uname;
this.pwd = pwd;
}
public User() {
}
}
Controller
package com.lg.springboot01.controller;
import com.lg.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 废废
* @site
* @company xxx公司
* @create 2019-12-27 20:08
*
* 介绍thymeleaf模板引擎
*/
@Controller
@RequestMapping("/thymeleaf")
public class UserController {
@RequestMapping("/list")
public ModelAndView list(){
System.out.println("list......");
ModelAndView mv=new ModelAndView();
mv.addObject("title","员工列表");
List<User> list =new ArrayList<>();
list.add(new User(""+1,"废废",123+""));
list.add(new User(""+2,"蔡的死",122+""));
list.add(new User(""+3,"胖虎",333+""));
mv.addObject("user",list);
mv.addObject("msg","<span style='color:red'>优秀员工zy<span>");
mv.setViewName("list");
return mv;
}
}
前台:
<!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" width="600px">
<caption th:text="${title}"></caption>
<tr>
<th>ID</th>
<th>名字</th>
<th>密码</th>
</tr>
<tr th:each="u : ${user}">
<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 模板
导入pom依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
<!--可以不加,但是做项目的时候可能会用-->
<resources>
<!--解决mybatis-generator-maven-plugin运行时没有将XxxMapper.xml文件放入target文件夹的问题-->
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
<!--freemarker模板也读取需要注释标红地方-->
<resource>
<directory>src/main/resources</directory>
<includes>
<!--<include>*.properties</include>-->
<!--<include>*.xml</include>-->
<!--<include>*.yml</include>-->
</includes>
</resource>
</resources>
Role
package com.lg.springboot01.entity;
import lombok.Data;
/**
* @author 废废
* @site
* @company xxx公司
* @create 2019-12-27 20:29
*/
@Data
public class Role {
private String rid;
private String rname;
public Role(String rid, String rname) {
this.rid = rid;
this.rname = rname;
}
public Role() {
}
}
RoleController
package com.lg.springboot01.controller;
import com.lg.springboot01.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 废废
* @site
* @company xxx公司
* @create 2019-12-27 20:08
*
* 介绍thymeleaf模板引擎
*/
@Controller
@RequestMapping("/freemarker")
public class RoleController {
@RequestMapping("/list")
public ModelAndView list(){
System.out.println("list......");
ModelAndView mv=new ModelAndView();
mv.addObject("name","废废");
mv.addObject("sex","gay");
List<Role> list =new ArrayList<>();
list.add(new Role("1","前台"));
list.add(new Role("2","财务"));
list.add(new Role("3","人事"));
mv.addObject("roles",list);
mv.setViewName("list");
return mv;
}
}
list
<!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>
</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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>底部版权信息</title>
</head>
<body>
阿里巴巴集团| 淘宝网 | 天猫 | 聚划算 | 全球速卖通 | 阿里巴巴国际交易市场| 1688 | 阿里妈妈 | 飞猪 | 阿里云计算 | AliOS | 阿里通信 | 万网 | 高德 | UC | 友盟 | 虾米 | 钉钉 | 支付宝
增值电信业务经营许可证: 浙B2-20110446 市场名称登记证:工商网市字3301004119号 出版物网络交易平台服务经营备案证: 新出发浙备字第001号
互联网违法和不良信息举报电话:0571-81683755 blxxjb@alibaba-inc.com 互联网药品信息服务资质证书编号:浙-(经营性)-2017-0005 浙公网安备 33010002000120号
医疗器械网络交易服务第三方平台备案:(浙)网械平台备字[2018]第00002号 互联网药品交易服务资格证书:国A20150001
浙江省网络食品销售第三方平台提供者备案:浙网食A33010002 12318举报
© 2003-2019 TMALL.COM 版权所有
</body>
</html>
页面显示: