一起来学SpringBoot | 第四篇:整合Thymeleaf模板

SpringBoot 是为了简化 Spring 应用的创建、运行、调试、部署等一系列问题而诞生的产物,自动装配的特性让我们可以更好的关注业务本身而不是外部的XML配置,我们只需遵循规范,引入相关的依赖就可以轻易的搭建出一个 WEB 工程

在前面几章中已经介绍了如何创建一个 SpringBoot 项目,同时简单的描述了 SpringBoot REST Web 服务。除此之外它也是支持如JSPThymeleafFreeMarkerMustacheVelocity 等各种模板引擎,同时还为开发者提供了自定义模板扩展的支持。

使用嵌入式Servlet容器时,请避免使用JSP,因为使用JSP打包后会存在一些限制。

SpringBoot 使用上述模板,默认从 src/main/resources/templates 下加载。

thymeleaf介绍

Thymeleaf 是现代化服务器端的Java模板引擎,不同与其它几种模板的是 Thymeleaf 的语法更加接近HTML,并且具有很高的扩展性。详细资料可以浏览官网

特点

  • 支持无网络环境下运行,由于它支持 html 原型,然后在 html 标签里增加额外的属性来达到模板+数据的展示方式。浏览器解释 html 时会忽略未定义的标签属性,所以 thymeleaf 的模板可以静态地运行;当有数据返回到页面时,Thymeleaf 标签会动态地替换掉静态内容,使页面动态显示。所以它可以让前端小姐姐在浏览器中查看页面的静态效果,又可以让程序员小哥哥在服务端查看带数据的动态页面效果。
  • 开箱即用,为 Spring 提供方言,可直接套用模板实现 JSTLOGNL 表达式效果,避免每天因套用模板而修改JSTLOGNL标签的困扰。同时开发人员可以扩展自定义的方言。
  • SpringBoot官方推荐模板,提供了可选集成模块(spring-boot-starter-thymeleaf),可以快速的实现表单绑定、属性编辑器、国际化等功能。
使用

首先要在 pom.xml 中添加对 thymeleaf 模板依赖

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

然后创建一个 ThymeleafController 用来映射HTTP请求与页面的跳转,下面写了两种方式,第一种比较直观和优雅,第二种相对普遍且代码较少,且迎合从 struts2 跳坑的朋友们…

  • Spring4.3以后为简化@RequestMapping(method = RequestMethod.XXX)的写法,故而将其做了一层包装,也就是现在的GetMappingPostMappingPutMappingDeleteMappingPatchMapping
package com.lpf.chapter3.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping
public class ThymeleafController {

    @GetMapping("/index")
    public ModelAndView index() {
        ModelAndView view = new ModelAndView();
        // 设置跳转的视图 默认映射到 src/main/resources/templates/{viewName}.html
        view.setViewName("index");
        // 设置属性
        view.addObject("title", "我的第一个WEB页面");
        view.addObject("desc", "欢迎进入lpf-web 系统");
        Author author = new Author();
        author.setAge(22);
        author.setEmail("394969568@qq.com");
        author.setName("挖坑埋你");
        view.addObject("author", author);
        return view;
    }

    @GetMapping("/index1")
    public String index1(HttpServletRequest request) {
        // TODO 与上面的写法不同,但是结果一致。
        // 设置属性
        request.setAttribute("title", "我的第一个WEB页面");
        request.setAttribute("desc", "欢迎进入lpf-web 系统");
        Author author = new Author();
        author.setAge(22);
        author.setEmail("394969568@qq.com");
        author.setName("挖坑埋你");
        request.setAttribute("author", author);
        // 返回的 index 默认映射到 src/main/resources/templates/xxxx.html
        return "index";
    }

    class Author {
        private int age;
        private String name;
        private String email;

        public int getAge() {
            return age;
        }

        public void setAge(int age) {
            this.age = age;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getEmail() {
            return email;
        }

        public void setEmail(String email) {
            this.email = email;
        }
    }

}

最后在 src/main/resources/templates 目录下创建一个名 index.html 的模板文件,可以看到 thymeleaf 是通过在标签中添加额外属性动态绑定数据的

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <!-- 可以看到 thymeleaf 是通过在标签里添加额外属性来绑定动态数据的 -->
    <title th:text="${title}">Title</title>
    <!-- 在/resources/static/js目录下创建一个hello.js 用如下语法依赖即可-->
    <script type="text/javascript" th:src="@{/js/hello.js}"></script>
</head>
<body>
    <h1 th:text="${desc}">Hello World</h1>
    <h2>=====作者信息=====</h2>
        <p th:text="${author?.name}"></p>
        <p th:text="${author?.age}"></p>
        <p th:text="${author?.email}"></p>
</body>
</html>

静态效果

双击打开 index.html 既可以看到如下的静态效果,并未和其它模板一样显示一堆标签的内容,而是正常渲染静态页面
这里写图片描述

动态效果

在浏览器输入:http://localhost:8080/index 可以看到渲染后的效果,真正意义上的动静分离了

这里写图片描述

小技巧

模板热部署

IntelliJ IDEA 中使用 thymeleaf 模板的时候,发现每次修改静态页面都需要重启才生效,这点是很不友好的,百度了下发现原来是默认配置搞的鬼,为了提高响应速度,默认情况下会缓存模板。如果是在开发中请将spring.thymeleaf.cache 属性设置成 false。在每次修改静态内容时按Ctrl+Shift+F9即可重新加载了…

修改默认 favicon.ico 图标

默认情况下使用 springboot 总能看到一片叶子,这是因为我们没配置自己的 ico 导致的,解决方法也很简单,只需要在 src/main/static/ 目录下放置一张名为 favicon.ico 就可以了

默认配置

SpringBoot 默认情况下为我们做了如下的默认配置工作,熟悉默认配置在开发过程中可以更好的解决问题
这里写图片描述

总结

Thymeleaf参考手册:https://blog.csdn.net/zrk1000/article/details/72667478

WEB MVC详细的内容请参考官方文档:https://docs.spring.io/spring/docs/5.0.5.RELEASE/spring-framework-reference/web.html#mvc

目前很多大佬都写过关于 SpringBoot 的教程了,如有雷同,请多多包涵,本教程基于最新的 spring-boot-starter-parent:2.0.1.RELEASE 编写,包括新版本的特性都会一起介绍…



源码地址:一起来学SpringBoot | 第四篇:整合Thymeleaf模板





跟着原作者的步骤一点一点敲过来,所以有些地方强迫症式的做了修改,请勿见怪。
来源:http://blog.battcn.com/2018/04/22/springboot/v2-config-properties/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值