1、springboot之thymeleaf模板
关于Thymeleaf的优点,我只说一条:它就是html页面。下面直接上代码
相关pom依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
Spring Boot官方文档建议在开发时将缓存关闭,那就在application.properties文件中加入下面这行
spring.thymeleaf.cache=false
正式环境还是要将缓存开启的
<html xmlns:th="http://www.thymeleaf.org">
package com.xxx.springboot02.controller;
import com.xxx.springboot02.entity.Role;
import com.xxx.springboot02.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 Zhang
*/
@Controller
public class IndexController {
@RequestMapping("/user/list")
public ModelAndView list(){
ModelAndView modelAndView=new ModelAndView();
modelAndView.setViewName("user/list");
modelAndView.addObject("title","用户列表");
List list=new ArrayList();
list.add(new User(1,"aaa","hahah"));
list.add(new User(2,"大冬瓜","chi"));
modelAndView.addObject("users",list);
return modelAndView;
}
list.html
<!DOCTYPE html>
<html lang="en">
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>用户列表</title>
</head>
<body>
<h1 th:text="${title}"></h1>
<h1>循环</h1>
<table border="1px" width="600px">
<thead>
<tr>
<td>用户id</td>
<td>用户名</td>
<td>用户描述</td>
</tr>
</thead>
<tbody>
<tr th:each="user : ${users}">
<td th:text="${user.uid}"></td>
<td th:text="${user.userName}"></td>
<td th:text="${user.desc}"></td>
</tr>
</tbody>
</table>
<h1>下拉框</h1>
<select>
<option th:each="user : ${users}" th:value="${user.uid}" th:text="${user.userName}"></option>
</select>
</body>
</html>
添加freemarker
list.ftl
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>角色列表</title>
<#include 'common.ftl'>
</head>
<body>
<h1>取值</h1>
<#--!:如果有值展示的是你写值,为null,则展示未知-->
welcome 【${name!'未知'}】 to page
<h1>非空判断</h1>
<#if name?exists>
xxx
</#if>
<h1>条件表达式</h1>
<#if sex=='boy'>
男
<#elseif sex=='gril'>
女
<#else>
保密
</#if>
<h1>循环</h1>
<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.roleName}</td>
<td>${role.desc}</td>
</tr>
</#list>
</tbody>
</table>
<h1>获取项目名(设置局部变量/全局变量)</h1>
<#--c:set-->
<#--将项目名赋给ctx1这个变量,这边作用域在当前页面-->
<#assign ctx1>
${springMacroRequestContext.contextPath}
<#-- ${springMacroRequestContext.contextPath}相当于jsp${pageContext.request,contextPath}-->
</#assign>
<#--将项目名赋给ctx2这个变量,这边作用域在整个项目-->
<#global ctx2>
${springMacroRequestContext.contextPath}
</#global>
${ctx1},${ctx2}
<h1>包含(include)</h1>
<#include 'foot.ftl'>
</body>
</html>
静态资源(js/css...)
<#assign ctx>
${springMacroRequestContext.contextPath}
</#assign>
<base href="${ctx}/">
<script type="text/javascript" src="static/js/layuixx.js"></script>
跳转页面必须经过后台
底部内容
<a href="login.ftl">登录1</a><!--错误-->
<a href="${ctx1}/login">登录2</a>