一、springboot项目默认文件位置
1.文件目录
- /src/java/resources :Maven的资源文件目录
- /src/java/resources/static
- /src/java/resources/public
- /src/java/resources/templates
- /src/java/resources/META-INF/resources
2.springboot静态首页的支持
默认情况下,springboot回到上面文件目录路径中找index.html(注意:controller注解不能使用@RestController,要用@Controller,因为@RestController =@Controller+@ResponeBody,会直接返回josn数据)
即
classpath:/META-INF/resources/index.html classpath:/resources/index.html classpath:/static/index.html calsspath:/public/index.html
如果想将index.html放到别的路径下,例如/src/java/resources/templates目录。
在controller中指定跳转的**页面名称**index
@RequestMapping("/") public String index(){ return "index"; }
配置文件中修改映射时视图的前后缀(前缀prefix是”classpath:/templates/”,后缀suffix是”.html” )
spring.thymeleaf.prefix: classpath:/templates/ spring.thymeleaf.suffix: .html
这样springboot就可以找到/src/java/resources/templates/index.html
二、thymeleaf 3+ 整合到springboot
由于springboot默认整合的thymeleaf 是2.0.0.RELEASE版本(我使用的springboot是当前稳定版2.0.0.RELEASE)
<groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> <version>2.0.0.RELEASE</version> <name>Spring Boot Thymeleaf Starter</name> <description>Starter for building MVC web applications using Thymeleaf views</description> <url>https://projects.spring.io/spring-boot/#/spring-boot-parent/spring-boot-starters/spring-boot-starter-thymeleaf</url>
添加thymeleaf 3 依赖,并改版本
<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> <!-- 默认使用Thymeleaf 2.0.0.RELEASE,这里需要使用Thymeleaf 3版本--> <!-- 低版本的 Thymeleaf 不能解析 HTML5 格式的 HTML 代码,而是把 HTML 当成 XML 解析 --> <thymeleaf.version>3.0.9.RELEASE</thymeleaf.version> </properties> <dependencies> <!-- 基础组件--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- 测试组件 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!-- 热部署组件 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <optional>true</optional> <scope>true</scope> </dependency> <!-- thymeleaf模板引擎 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <fork>true</fork><!-- fork:如果没有该配置,这个devtools不会起作用,即应用不会restart --> </configuration> </plugin> </plugins> </build>
spring-boot-starter-web依赖删不删除看清情况
如果thymeleaf中没有web模块,就不需要删除wen
如果thymeleaf中自带了web,就可以删除。
三、使用实例
1.在html页面中引入thymeleaf命名空间
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>testThymeleaf</h1>
</body>
</html>
2.测试http://localhost:8080/
四、关于Thymeleaf缓存禁用问题
- 我使用devtools热部署
- 就不需要禁掉模板引擎的缓存了(spring.thymeleaf.cache: false )
- 使用devTool热部署感觉六的飞起