一)使用Thymelea进行数据的展示
1.pom文件
<?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</modelVersion>
<groupId>com.sxt</groupId>
<artifactId>03-spring-boot-tym</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.7.RELEASE</version>
<relativePath/>
</parent>
<dependencies>
<!-- springBoot 的启动器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- springBoot 的thymeleaf组件 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
</dependencies>
</project>
二)Thymelea
模板引擎
1、市面上主流的 Java 模板引擎有:JSP、Velocity、Freemarker、Thymeleaf
2、JSP 本质也是模板引擎,Spring Boot 官方推荐使用 “Thymeleaf”模板引擎
3、模板引擎原理图如下,模板引擎的作用都是将模板(页面)和数据进行整合然后输出显示,区别在于不同的模板使用不同的语法,如 JSP 的JSTL表达式,以及J SP 自己的表达式和语法,同理 Thymeleaf 也有自己的语法
Thymelea:就是一种模板渲染技术,它使用特定的语法对html进行渲染,也就是说我们可以直接使用html做为视图层技术
Thymelea的使用
1.引入Thymelea坐标
2.在src/main/resources目录创建templates目录(默认为templates名字)
3.Controller
package com.sxt.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.jws.WebParam;
/**
* @project_name:hibernate02
* @date:2019/8/22:17:49
* @author:shinelon
* @Describe:
*/
@Controller
public class IndexController {
@RequestMapping("/show")
public String show(Model model){
model.addAttribute("msg", "thymelear入门案例");
return "index";
}
}
4.视图
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Thymelear入门案例</title>
</head>
<body>
<!--th:text 标签相当于输出-->
<span th:text="hello"></span>
<hr/>
<!-- 取控制器传递的值的方式和el表达式类似-->
<span th:text="${msg}"></span>
</body>
</html>
5.页面显示效果
低版本的对html标签的格式要求是非常严谨的,标签必须有开始和结束,如果出现标签语法的问题,则是需要添加结束标签或则提高Thymelea版本,高版本允许html出现不标准的语法格式。