
先举一个例子,然后再介绍一下,看了更多理论后很容易晕眩
在资源目录下的模板中创建一个新的html文件

代码: 请注意html标记与jsp或html之间的区别,请自行添加

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/>
</head>
<body>
<h1>这是你的thymeleaf页面</h1>
<span th:text="'你是:'+${key}">SB</span>
<table>
<tr>
<th>NAME</th>
<th>PRICE</th>
<th>IN STOCK</th>
</tr>
<tr th:each="prod : ${comments}">
<td th:text="${prod.name}">Onions</td>
<td th:text="${prod.price}">2.41</td>
<td th:text="${prod.age}">yes</td>
</tr>
</table>
</body>
</html>
build.gradle加入百里香依赖性
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
创建一个新的实体类

package com.comment.bean;
/***
*
* <p>TODO评论实体类</p>
*
* <p>Copyright: 版权所有 (c) 2002 - 2008<br>
*
* @author ainy
* @version 2019年2月15日
*/
public class Comment {
private String name;
private int age;
private double price;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
}
创建一个新的控制器
package com.comment.consumer;
import java.util.ArrayList;
import java.util.List;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.comment.bean.Comment;
@Controller
public class PageController {
@RequestMapping(value = "show", method = RequestMethod.GET)
public String show(Model model){
model.addAttribute("key","Jerry");
List<Comment> comments=new ArrayList<>();
Comment c1=new Comment();
c1.setAge(8);
c1.setName("李晓明");
c1.setPrice(99);
comments.add(c1);
Comment c2=new Comment();
c2.setAge(37);
c2.setName("王尼玛");
c2.setPrice(66);
comments.add(c2);
model.addAttribute("comments", comments);
return "hello.html";
}
}
开始进入课程
如图所示直接访问URL

直接在文件夹中打开网页,该文件夹是静态网页

效果:

比较打开静态页面和打开动态页面之间的区别
您将在静态页面上显示的标签中间找到内容:

动态打开显示从后台传递的数据,并且不再显示标签中的数据. 这是分离前端和后端的好方法. 它类似于jsp. 如果使用Spring Bootjsp好空间,尽管可以使用jsp,但不要使用它.jsp好空间,但官方不推荐
本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/tongxinshuyu/article-280202-1.html
同感
写的不好怪谁