Spring boot入门系列(二):使用Thymeleaf模板引擎开发Web应用简单示例
2017-10-23 13:48
363 查看
前言:本文所用jdk版本为:jdk1.8.0_111版本;Spring boot1.5.8版本。
/static
/public
/resources
META-INF/resources
我们可以在原resources/路径下创建static路径用于存放静态的文件,如:图片、样式表等。
本文所采用的引擎模板为Thymeleaf。
默认的模板配置路径为:classpath:templates/
一、静态资源路径
上一篇文章搭建了一个简易web框架,可能很多人很奇怪既然是web应用,没有了webContent、webapp等路径,前端页面需要的资源如css文件、js、html等放在哪里呢?2.1 默认配置
Spring boot默认的静态资源访问路径是在classpath下的,目录名需要符合以下规则:/static
/public
/resources
META-INF/resources
我们可以在原resources/路径下创建static路径用于存放静态的文件,如:图片、样式表等。
2.2 自定义配置
如果不想用Spring boot提供的默认路径,可以在application.properties文件中使用spring.resources.static-locations参数重新配置路径。该参数是用于覆盖默认静态资源路径的,注意,这里是“覆盖”,所以如果想保留原默认配置的话,需要在这里加上原有默认的路径。二、模板引擎
Spring boot提供了几种默认配置的模板引擎,如:Thymeleaf、Freemark、Velocity、Groovy等。(注意:其中Velocity在1.5.x版本后便不再支持了)本文所采用的引擎模板为Thymeleaf。
默认的模板配置路径为:classpath:templates/
2.1 添加依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>
2.2 创建简单html页面示例
我们创建目录在src/main/resources/tempaltes/ ,并在该目录下创建文件index.html<!DOCTYPE html> <html> <head> <meta charset="UTF-8" /> <title>欢迎页面</title> </head> <body> <h1 th:text="${time}"></h1> </body> </html>
2.3 添加一个Controller示例
@GetMapping("/") public String index(ModelMap map) { map.addAttribute("time", new Date()); return "index"; }完成后启动主程序,在浏览器中访问 http://localhost:8080 , 如果正常,将会在页面看到打印出来的时间。
相关文章推荐
- Spring Boot 系列 - web开发 Thymeleaf、FreeMarker模板引擎
- Spring Boot 系列(五)web开发-Thymeleaf、FreeMarker模板引擎
- SpringBoot入门系列篇(五):简单使用thymleaf模板
- 深入学习spring-boot系列(三)--使用thymeleaf模板
- 深入学习spring-boot系列(三)--使用thymeleaf模板
- 深入学习spring-boot系列(三)--使用thymeleaf模板
- 深入学习spring-boot系列(三)--使用thymeleaf模板
- JavaEE开发之SpringBoot整合MyBatis以及Thymeleaf模板引擎
- JavaEE开发之SpringBoot整合MyBatis以及Thymeleaf模板引擎
- 深入学习spring-boot系列(三)--使用thymeleaf模板
- 【java开发系列】—— spring简单入门示例
- 深入学习spring-boot系列(三)--使用thymeleaf模板
- 【java开发系列】—— spring简单入门示例
- (8)spring boot使用thymeleaf模板引擎
- 深入学习spring-boot系列(三)--使用thymeleaf模板
- 深入学习spring-boot系列(三)--使用thymeleaf模板
- 【java开发系列】—— spring简单入门示例
- 深入学习spring-boot系列(三)--使用thymeleaf模板
- 深入学习spring-boot系列(三)--使用thymeleaf模板
- 深入学习spring-boot系列(三)--使用thymeleaf模板