thymeleaf介绍
简单说, Thymeleaf 是一个跟 Velocity、FreeMarker 类似的模板引擎,它可以完全替代 JSP 。相较与其他的模板引擎,它有如下三个极吸引人的特点:
-
1.Thymeleaf 在有网络和无网络的环境下皆可运行,即它可以让美工在浏览器查看页面的静态效果,也可以让程序员在服务器查看带数据的动态页面效果。这是由于它支持 html 原型,然后在 html 标签里增加额外的属性来达到模板+数据的展示方式。浏览器解释 html 时会忽略未定义的标签属性,所以 thymeleaf 的模板可以静态地运行;当有数据返回到页面时,Thymeleaf 标签会动态地替换掉静态内容,使页面动态显示。
-
2.Thymeleaf 开箱即用的特性。它提供标准和spring标准两种方言,可以直接套用模板实现JSTL、 OGNL表达式效果,避免每天套模板、该jstl、改标签的困扰。同时开发人员也可以扩展和创建自定义的方言。
-
3.Thymeleaf 提供spring标准方言和一个与 SpringMVC 完美集成的可选模块,可以快速的实现表单绑定、属性编辑器、国际化等功能。
Spring boot整合Thymeleaf非常简单,只需以下几个步骤:
1.引入依赖
<!-- thymeleaf依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!-- 自定义thymeleaf版本,可有可无 -->
<!-- <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<!-- set thymeleaf version -->
<thymeleaf.version>3.0.0.RELEASE</thymeleaf.version>
<thymeleaf-layout-dialect.version>2.0.0</thymeleaf-layout-dialect.version>
</properties> -->
2.创建html模板文件,
spring-boot很多配置都有默认配置,比如默认页面映射路径为
classpath:/templates/*.html
同样静态文件路径为 classpath:/static/
同样静态文件路径为 classpath:/static/
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h4>亲爱的<span th:text="${name}"></span>,你好!</h4>
</body>
</html>
3.
在application.properties中可以配置thymeleaf模板解析器属性
#### thymeleaf配置 #######
#thymeleaf start
spring.thymeleaf.mode=HTML5
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.content-type=text/html
#开发时关闭缓存,不然没法看到实时页面
spring.thymeleaf.cache=false
#thymeleaf end
4.创建一个controller
package com.ziyu.thymeleaf;
import java.util.Map;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/thymeleaf")
public class ThymeleafController {
@RequestMapping("/hello")
public String hello(Map<String, Object> map) {
map.put("name", "fanfan");
return "hello";
}
}
5.创建App.java启动类
package com.ziyu.thymeleaf;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@SpringBootApplication(exclude = { RedisAutoConfiguration.class })
@Configuration
@EnableAutoConfiguration
public class App{
public static void main(String[] args) {
SpringApplication app = new SpringApplication(HelloApplication.class);
app.setBannerMode(org.springframework.boot.Banner.Mode.OFF);
app.run(args);
}
}
6.运行App.java,浏览器输入地址: http://localhost:8080/thymeleaf/hello
这样就完成啦!
放一张我的目录结构
回味上面的DEMO,可以看出来首先要在改写html标签
这样的话才可以在其他标签里面使用 th:text 这样的语法
在做整合的时候我遇到了几个问题:
当我只引入thymeleaf依赖的时候运行App,报了这个错误
Exception in thread "main" java.lang.IllegalStateException: Failed to read Class-Path attribute from manifest of jar file:/H:/apache-maven-3.3.9-bin/repository/repository/org/thymeleaf/thymeleaf/2.1.5.RELEASE/thymeleaf-2.1.5.RELEASE.jar
at org.springframework.boot.devtools.restart.ChangeableUrls.getUrlsFromClassPathOfJarManifestIfPossible(ChangeableUrls.java:110)
at org.springframework.boot.devtools.restart.ChangeableUrls.fromUrlClassLoader(ChangeableUrls.java:96)
at org.springframework.boot.devtools.restart.DefaultRestartInitializer.getUrls(DefaultRestartInitializer.java:93)
at org.springframework.boot.devtools.restart.DefaultRestartInitializer.getInitialUrls(DefaultRestartInitializer.java:56)
at org.springframework.boot.devtools.restart.Restarter.<init>(Restarter.java:140)
at org.springframework.boot.devtools.restart.Restarter.initialize(Restarter.java:546)
at org.springframework.boot.devtools.restart.RestartApplicationListener.onApplicationStartingEvent(RestartApplicationListener.java:67)
at org.springframework.boot.devtools.restart.RestartApplicationListener.onApplicationEvent(RestartApplicationListener.java:45)
at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:167)
at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:139)
at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:122)
at org.springframework.boot.context.event.EventPublishingRunListener.starting(EventPublishingRunListener.java:68)
at org.springframework.boot.SpringApplicationRunListeners.starting(SpringApplicationRunListeners.java:48)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:303)
at com.ziyu.thymeleaf.HelloApplication.main(HelloApplication.java:22)
Caused by: java.util.zip.ZipException: invalid LOC header (bad signature)
at java.util.zip.ZipFile.read(Native Method)
at java.util.zip.ZipFile.access$1400(Unknown Source)
at java.util.zip.ZipFile$ZipFileInputStream.read(Unknown Source)
at java.util.zip.ZipFile$ZipFileInflaterInputStream.fill(Unknown Source)
at java.util.zip.InflaterInputStream.read(Unknown Source)
at sun.misc.IOUtils.readFully(Unknown Source)
at java.util.jar.JarFile.getBytes(Unknown Source)
at java.util.jar.JarFile.getManifestFromReference(Unknown Source)
at java.util.jar.JarFile.getManifest(Unknown Source)
at org.springframework.boot.devtools.restart.ChangeableUrls.getUrlsFromClassPathOfJarManifestIfPossible(ChangeableUrls.java:107)
... 14 more
把H:\apache-maven-3.3.9-bin\repository\repository\org\thymeleaf\thymeleaf也就是本地仓库下的2.1.5.RELEASE目录删掉再maven update一下就好了
更新完成后再运行App主类,又报了这个错误:
顾名思义,html模板里<meta>标签没有结束标签,添加上去就好了
如果是 thymeleaf 3.0版本,那就可以没有结束标签,如果要用3.0版本,就需要用到最开始的自定义版本了
除此之外,当我更新完maven再运行spring boot的时候,报了这个错误:
查了很多资料,最后用cmd cd进入这个项目所在的目录下,执行mvn package 命令
就可以正常运行了。。