目录
1.整合Thymeleaf
Thyme leaf 是新一代Java 模板引擎,类似于Velocity、FreeMarker 等传统Java 模板引擎。与传
统Java 模板引擎不同的是, Thymeleaf 支持HTML 原型,既可以让前端工程师在浏览器中直接打
开查看样式, 也可以让后端工程师结合真实数据查看显示效果。同时, Spring Boot 提供了Thymeleaf
自动化配置解决方案,因此在Spring Boot 中使用Thymeleaf 非常方便。Spring Boot 整合Thymeleaf
主要可通过如下步骤:
1.新建spring Boot工程,添加依赖
<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>
2.配置Thymeleaf
Spring Boot为Thymeleaf 提供了自动化配置类ThymeleafAutoConfiguration,相关配置属性在ThymeleafProperties类中,ThymeleafProperties部分源码如下:
@ConfigurationProperties(prefix = "spring.thymeleaf")
public class ThymeleafProperties{
pravite static final Charset DEFAULT_ENCODING=StandardCharsets.UTF_8;
public static final String DEFAULT_PREFIX="classpath:/template/";
public static final String DEFAULT_SUFFIX=".html";
...
...
}
#是否开启缓存,开发时设置为false,默认为true
spring.thymeleaf.cache=true
#检查模板是否存在,默认为true
spring。thymeleaf.check-template=true
#检查模板位置是否存在,默认为true
spring。thymeleaf.check-template-location=true
#模板文件编码
spring.thymeleaf.encoding=UTF-8
#模板文件位置
spring.thymeleaf。prefix=classpath:/template/
#Content-Type配置
spring.thymeleaf.servlet.content-type=text/html
#模板文件后缀
spring.thymeleaf.suffix=.html
配置控制器
创建Book
package org.sang.chapter03;
public class Book {
private Integer id;
private String name;
private String author;
//省略getter/setter
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
}
创建BookController
package org.sang.chapter03;
import java.util.List;
import java.util.ArrayList;
import org.sang.chapter03.Book;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class BookController {
@GetMapping("/books")
public ModelAndView books()
{
List<Book> books = new ArrayList<>();
Book b1 = new Book();
b1.setId(1);
b1.setAuthor("罗贯中");
b1.setName("三国演义");
Book b2 = new Book();
b2.setId(2);
b2.setAuthor("曹雪芹");
b2.setName("红楼梦");
books.add(b1);
books.add(b2);
ModelAndView mv=new ModelAndView();
mv.addObject("books", books);
mv.setViewName("books");
return mv;
}
}
创建视图
在resources 目录下的templates 目录中创建books.html , 具体代码如下:
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>图书列表</title>
</head>
<body>
<table border="1">
<tr>
<td>图书编号</td>
<td>图书名称</td>
<td>图书作者</td>
</tr>
<tr th:each="book:${books}">
<td th:text="${book.id}"></td>
<td th:text="${book.name}"></td>
<td th:text="${book.author}"></td>
</tr>
</table>
</body>
</html>
2.整合FreeMarker
配置项目,添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId> spring-boot-starter-freemarker</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId> spring-boot-starter-web</artifactId>
</dependency>
配置FreeMarker
#修改FreeMarker 默认配置
#HttpServletRequest的属性是否可以覆盖controller中model的同名项
spring.freemarker.allow-request-override=false
#HttpSession的属性是否可以覆盖controller中model的同名项
spring.freemarker.allow-session-override=false
#是否开放缓存
spirng.freemarker.cache=false
#模板文件编码
spirng.freemarker.charser=UTF-8
#是否检查模板位置
spring.freemarker.check-template-location=true
#Content-Type的值
spring.freemarker.content-type=text/html
#是否将HttpServletRequest中的属性添加到Model中
spring.freemarker.expose-request-attributes=false
#是否将HttpSession中的属性添加到model中
spring.freemarker.expose-session-attributes=false
#模板文件后缀
spring.freemarker.suffix=.ftl
#模板文件位置
spring.freemarker.template-loader-path=classpath:/templates/
配置控制器,同thymeleaf
创建视图
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>图书列表</title>
</head>
<body>
<table border="1">
<tr>
<td>图书编号</td>
<td>图书名称</td>
<td>图书作者</td>
</tr>
<#if books ??&&(books?size>0)>
<#list books as book>
<tr>
<td> ${book.id}</td>
<td> ${book.name}</td>
<td> ${book.author}</td>
</tr>
</#list>
</#if>
</table>
</body>
</html>