SpringBoot支持的前端模板技术有Thymeleaf、FreeMarker、Jsp等,一般标配为Thymeleaf(官方建议),也可用FreeMarker。不建议使用Jsp,因为有些功能不能使用。
Jsp局限:
- 如果您使用 war 打包,在 Jetty 和 Tomcat 中可以正常工作,使用
java -jar
启动时,可执行的 war 可正常使用,并且还可以部署到任何标准容器。使用可执行 jar 时不支持 JSP。 - Undertow 不支持 JSP。
- 创建自定义的 error.jsp 页面不会覆盖默认错误处理视图,应该使用自定义错误页面来代替。
模板默认从 src/main/resources/templates获取。这里只介绍Thymeleaf和FreeMarker。
Thymeleaf
Thymeleaf是一个用于编写动态页面模板引擎,可以直接html文本,达到快速渲染页面的目的。
依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
thymeleaf的配置
spring.thymeleaf.cache=true #启用模板缓存。
spring.thymeleaf.check-template=true #在呈现模板之前检查模板是否存在。
spring.thymeleaf.check-template-location=true #检查模板位置是否存在。
spring.thymeleaf.content-type=text / html #Content-Type值。
spring.thymeleaf.enabled=true #启用MVC Thymeleaf视图分辨率。
spring.thymeleaf.encoding=UTF-8 #模板编码。
spring.thymeleaf.excluded-view-names= #应该从解决方案中排除的视图名称的逗号分隔列表。
spring.thymeleaf.mode=HTML5 #应用于模板的模板模式。另请参见StandardTemplateModeHandlers。
spring.thymeleaf.prefix=classpath:/ templates / #在构建URL时预先查看名称的前缀。
spring.thymeleaf.suffix=.html #构建URL时附加到查看名称的后缀。
spring.thymeleaf.template-resolver-order= #链中模板解析器的顺序。
spring.thymeleaf.view-names= #可以解析的视图名称的逗号分隔列表。/ templates / #在构建URL时先查看名称的前缀。
spring.thymeleaf.suffix =.html #构建URL时附加到查看名称的后缀。
spring.thymeleaf.template-resolver-order= #链中模板解析器的顺序。
spring.thymeleaf.view-names= #可以解析的视图名称的逗号分隔列表。/ templates / #在构建URL时先查看名称的前缀。
spring.thymeleaf.suffix= .html #构建URL时附加到查看名称的后缀。
spring.thymeleaf.template-resolver-order= #链中模板解析器的顺序。
spring.thymeleaf.view-names= #可以解析的视图名称的逗号分隔列表。
其中有值的则表示默认值。常见配置如下:
spring.thymeleaf.cache=true
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.mode=HTML5
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
demo案例
开发环境
- java版本:jdk1.8
- IDE:IntelliJ IDEA 2018.3.1
创建工程
1、用IDE创建springboot 项目,主要配置如下:
2、选择thymeleaf依赖。也可以不选,直接在pom文件中加依赖(FreeMarker同)
3、创建控制类及前端HTML文件
IndexController.java控制类,用来处理请求并返回
package com.example.demo.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
/**
* 〈一句话功能简述〉<br>
* 〈〉
*
* @author Danny
* @since 1.0.0
*/
@Controller
@RequestMapping("/v1.0")
public class IndexController {
@RequestMapping("/index")
public ModelAndView test(ModelAndView mv){
mv.setViewName("index");
mv.addObject("name","张三");
return mv;
}
}
index.html要访问的前端页面
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<p th:text="'欢迎' + ${name}"></p>
</body>
</html>
启动服务,访问 http://localhost:8080/v1.0/index
注意:这里没有去设置themeleaf模板相关配置,一切采用默认值,实际情况根据项目需求添加配置。
FreeMarker
FreeMarker 是一个基于模板生成文本输出的通用工具,使用纯 Java 编写,它被设计用来生成 HTML Web 页面。
依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
freemarker的配置
#设置是否允许HttpServletRequest属性重写(隐藏)控制器生成同名的模型属性。
spring.freemarker.allow-request-override=false
#设置是否允许HttpSession属性重写(隐藏)控制器生成同名的模型属性。
spring.freemarker.allow-session-override=false
#启用模板缓存
spring.freemarker.cache=false
# 模板编码
spring.freemarker.charset=UTF-8
# 检查模板位置是否存在
spring.freemarker.check-template-location=true
# 内容类型值
spring.freemarker.content-type=text/html
# 启用mvc视图解决方案
spring.freemarker.enabled=true
# 是否将所有请求属性添加到与模板合并之前的模型中
spring.freemarker.expose-request-attributes=false
# 设置是否所有HttpSession属性应该与模板融合之前添加到模型
spring.freemarker.expose-session-attributes=false
# 设置是否公开一个由Spring的macro库使用RequestContext,在名为“springMacroRequestContext”。
spring.freemarker.expose-spring-macro-helpers=true
# 是否开启模板文件的热部署
spring.freemarker.prefer-file-system-access=true
# 视图前缀
spring.freemarker.prefix=
# Name of the RequestContext attribute for all views.
spring.freemarker.request-context-attribute=
# Well-known FreeMarker keys which will be passed to FreeMarker's Configuration.
spring.freemarker.settings.*=
# 视图的后缀
spring.freemarker.suffix=.ftlh
# 模板路径配置,多个模板路径用分号分割
spring.freemarker.template-loader-path=classpath:/templates/
# 视图解析的白名单
spring.freemarker.view-names=
其中有值的则表示默认值。常见配置如下:
spring.freemarker.template-loader-path=classpath:/templates/
spring.freemarker.prefix=
spring.freemarker.suffix=.ftlh
spring.freemarker.charset=UTF-8
spring.freemarker.cache=false
demo案例
同thymeleaf案例一样,区别在于依赖、配置文件和前端文件,依赖不再多说
配置文件
spring:
freemarker:
suffix: .ftl
index.ftl前端文件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
欢迎${name}
</body>
</html>