第11章 构建Web应用
使用Spring Boot可以非常轻松地构建Web应用,也可轻松管理静态资源文件及页面模板。
新建Spring Boot应用时勾选Spring Boot MVC模块,便会发现在src/main/resources
目录下多出static
与templates
两个文件夹,前者用于存放静态文件,例如在static
文件中放入名为pic.jpg
的图片,则对应的访问地址为http://localhost:8080/pic.jpg
;而templates
中则用于存放模板文件,如*.jsp
等。
Spring Boot提供了默认配置的模板引擎主要有以下几种:
-
Thymeleaf
-
FreeMarker
-
Velocity
-
Groovy
-
Mustache
Spring Boot 官方并不推荐使用JSP模板引擎,所以这里以FreeMarker
为例进行讲解。
① 在pom.xml
中添加FreeMarker依赖。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
<dependency>
<groupId>o