SpringBoot第二篇:web(基于Thymeleaf模板)

接着第一篇,继续配置web项目。

1、在pom文件中加入:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

spring-boot-starter-thymeleaf:

  Spring Boot提供了默认配置的模板引擎主要有以下几种:

  1. Thymeleaf
  2. FreeMarker
  3. Velocity
  4. Groovy
  5. Mustache 

Spring Boot建议使用这些模板引擎,避免使用JSP。

  • Thymeleaf是一个XML/XHTML/HTML5模板引擎,可用于Web与非Web环境中的应用开发。它是一个开源的Java库,基于Apache License 2.0许可,由Daniel Fernández(Java加密库Jasypt的作者)创建。
  • Thymeleaf提供了一个用于整合Spring MVC的可选模块,在应用开发中,可以使用Thymeleaf来完全代替JSP或其他模板引擎,如Velocity、FreeMarker等。Thymeleaf的主要目标在于提供一种可被浏览器正确显示的、格式良好的模板创建方式,因此也可以用作静态建模。Thymeleaf 在有网络和无网络的环境下皆可运行,即它可以在浏览器查看页面的静态效果,也可以在服务器查看带数据的动态页面效果,这样非常有利于前后端的分离。这是由于它支持 html 原型,然后在 html 标签里增加额外的属性来达到模板+数据的展示方式。浏览器解释 html 时会忽略未定义的标签属性,所以 thymeleaf 的模板可以静态地运行;当有数据返回到页面时,Thymeleaf 标签会动态地替换掉静态内容,使页面动态显示。
  • Thymeleaf 开箱即用的特性。它提供标准和spring标准两种方言,可以直接套用模板实现JSTL、 OGNL表达式效果,避免每天套模板、该jstl、改标签的困扰。同时开发人员也可以扩展和创建自定义的方言。
  • Thymeleaf 提供spring标准方言和一个与 SpringMVC 完美集成的可选模块,可以快速的实现表单绑定、属性编辑器、国际化等功能。

spring boot建议的模板引擎默认的模板配置路径为:src/main/resources/templates

也可以修改模板配置路径(在src/main/resources/application.properties文件中修改):

#在构建URL时可以预览查看名称的前缀。注意结尾斜杠后不许存在字符,空格也不许存在。否则跳转页面会出错又看不出来

spring.thymeleaf.prefix=classpath:/templates/

# 在构建URL时附加到视图名称的后缀。

spring.thymeleaf.suffix=.html

模板的其他配置如下:

# Enable template caching.启用模板缓存。
spring.thymeleaf.cache=false 
# Check that the templates location exists.检查模板位置是否存在。
spring.thymeleaf.check-template-location=true 
# Content-Type value.内容类型值。
spring.thymeleaf.servlet.content-type=text/html 
# Enable MVC Thymeleaf view resolution.启用MVC TThymeleaf视图解析。
spring.thymeleaf.enabled=true 
# Template encoding.模板编码。
spring.thymeleaf.encoding=UTF-8
# Comma-separated list of view names that should be excluded from resolution.从解析中排除的视图名称的逗号分隔列表。
spring.thymeleaf.excluded-view-names=
# Template mode to be applied to templates. See also StandardTemplateModeHandlers.模板模式应用于模板。也请参阅标准模板。LEGACYHTML5
spring.thymeleaf.mode=LEGACYHTML5 
# Prefix that gets prepended to view names when building a URL.在构建URL时可以预览查看名称的前缀。
spring.thymeleaf.prefix=classpath:/templates/
# Suffix that gets appended to view names when building a URL.在构建URL时附加到视图名称的后缀。
spring.thymeleaf.suffix=.html

2、使用Thymeleaf模板

      在src/main/resources下创建templates文件夹(默认在此位置,用来存放模板页面html)

      在templates下创建一个html,如下:

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1 align="center"><span th:text="${test}"></span> Hello World</h1>
</body>
</html>

   在src/main/resources下创建static文件夹(默认在此位置,从来存放静态资源文件,如:js、css、图片等)

          在static下放置一个图片文件(test.jpg),启动项目可以直接访问:http://localhost:8080/test.jpg

3、在PageController.java中添加:

@RequestMapping("/page")
public String index(ModelMap map) {
    // 加入一个属性,用来在模板中读取
    map.addAttribute("test", "spring boot");
    // return模板文件的名称,对应src/main/resources/templates/page.html
    return "page";
}

4、启动项目,浏览器访问:http://localhost:8080/page,如图:

猜你喜欢

转载自blog.csdn.net/FromTheWind/article/details/84502493
今日推荐