SpringBoot官方推荐使用Thymeleaf模板引擎做视图层开发;
<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.0modelVersion>
<groupId>com.kenewstargroupId>
<artifactId>springboot02artifactId>
<version>1.0-SNAPSHOTversion>
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.2.6.RELEASEversion>
parent>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-thymeleafartifactId>
dependency>
dependencies>
project>
package com.kenewstar.thymeleaf;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* @Author:kenewstar
* @Description: springboot整合thymeleaf, 第一个程序
* @Date:Created in 2020/4/23
*/
@Controller
public class IndexController {
@RequestMapping("/show")
public String show(Model model){
model.addAttribute("msg","thymeleaf学习");
return "index";
}
}
这是一个很普通的控制器,与以往并没有什么不同,当前台发起/show的请求时,控制器将msg字符串信息响应给用户;
首先必须在resources目录下创建一个名为templates的目录,我们将html页面放在此目录下,thymeleaf的文件后缀是.html;
代码如下:
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
<meta charset="UTF-8">
<title>thymeleaf学习title>
head>
<body>
<h4 th:text="${msg}">h4>
<hr/>
<p th:text="thymeleaf第一个程序">p>
body>
html>
上述代码中,将响应内容msg取出数据,通过th:text=" ",双引号中使用EL表达式,常量字符串直接使用即可,更多关于Thymeleaf的知识请参考官网;
代码如下:
package com.kenewstar.thymeleaf;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* @Author:kenewstar
* @Description: springboot启动类
* @Date:Created in 2020/4/23
*/
@SpringBootApplication
public class ThymeleafApp {
public static void main(String[] args) {
//启动springboot程序
SpringApplication.run(ThymeleafApp.class,args);
}
}
启动项目,在浏览器地址栏输入localhost:8080/show
我们可以看出响应数据被取出来了;
① th:text : 在页面中输出值
② th:value: 将值放在input中,等同于input中的value
<input type="text" name="name" th:value="abc"/>
① th:if :
<span th:if="${sex} == 1">
男
span>
<span th:if="${sex} == 0">
女
span>
② th:switch :
<div th:switch="${id}">
<span th:case="1">ID 为 1span>
<span th:case="2">ID 为 2span>
<span th:case="3">ID 为 3span>
div>
① th:each :
<table>
<tr>
<th>IDth>
<th>NAMEth>
<th>AGEth>
tr>
<tr th:each="u : ${list}">
<td th:text="${u.userid}">td>
<td th:text="${u.username}">td>
<td th:text="${u.userage}">td>
tr>
table>
可以与
其他更多标签请参考官方文档
① HttpServletRequest
//java代码
request.setAttribute("req", "HttpServletRequest");
<span th:text="${#httpServletRequest.getAttribute('req')}">span>
② HttpSession
//java代码
request.getSession().setAttribute("sess", "HttpSession");
<span th:text="${session.sess}">span>
③ ServletContext
request.getSession().getServletContext().setAttribute("app","Application");
<span th:text="${application.app}">span>
① 绝对路径
<a th:href="@{https://www.baidu.com}">绝对路径a>
② 相对路径
相对于项目的上下文的相对路径
<a th:href="@{/show}">相对路径a>
③ 在 url 中实现参数传递
<a th:href="@{/show(id=1,name=kenewstar)}">传参a>
相当于下面的
<a href="/show?id=1&name=kenewstar">传参</a>
④ 在 url 中通过 restful 风格进行参数传递
<a th:href="@{/path/{id}/show(id=1,name=zhagnsan)}">传参</a>
篇幅限制,只介绍这么多,详情请参考官方文档