美文网首页
spring-boot-thymeleaf简单demo

spring-boot-thymeleaf简单demo

作者: 艺超51iwowo | 来源:发表于2021-04-18 00:11 被阅读0次

尽管现在提倡前后端分离,但是对于一些面向后端的应用系统,由于缺少前端人力,只能自己搭建。所以,SpringBoot提供的模板引擎,非常有帮助。 在实际项目中,由于时间比较早,还在使用Veloctiy。但是在新版本的SpringBoot中,推荐的还是thymeleaf,所以简单写了个demo,有助于后续在项目中使用。

1、增加thymeleaf starter依赖
<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
2、配置thymeleaf

基本上SpringBoot对于thymeleaf属于开箱即用的方式,很多配置值使用默认值即可,这里主要介绍几个重要的参数。

// 通常本地调试时,可以设置为false,便于每次看到新的结果,线上可以设置为true 
{
      "name": "spring.thymeleaf.cache",
      "type": "java.lang.Boolean",
      "description": "Whether to enable template caching.",
      "sourceType": "org.springframework.boot.autoconfigure.thymeleaf.ThymeleafProperties",
      "defaultValue": true
 }
// content-type,由于是页面,所以使用text/html即可。
{
  "name": "spring.thymeleaf.servlet.content-type",
  "type": "org.springframework.util.MimeType",
  "description": "Content-Type value written to HTTP responses.",
  "sourceType": "org.springframework.boot.autoconfigure.thymeleaf.ThymeleafProperties$Servlet",
  "defaultValue": "text\/html"
}
// 模板文件的格式
{
  "name": "spring.thymeleaf.mode",
  "type": "java.lang.String",
  "description": "Template mode to be applied to templates. See also Thymeleaf's TemplateMode enum.",
  "sourceType": "org.springframework.boot.autoconfigure.thymeleaf.ThymeleafProperties",
  "defaultValue": "HTML"
}

可选列表:

image-20210418000623489
// 该参数用户设置thymeleaf模板文件的前缀,默认为classpath:templates路径即可。
 {
      "name": "spring.thymeleaf.prefix",
      "type": "java.lang.String",
      "description": "Prefix that gets prepended to view names when building a URL.",
      "sourceType": "org.springframework.boot.autoconfigure.thymeleaf.ThymeleafProperties",
      "defaultValue": "classpath:\/templates\/"
  }
// 对应模板文件的后缀,默认为html
{
      "name": "spring.thymeleaf.suffix",
      "type": "java.lang.String",
      "description": "Suffix that gets appended to view names when building a URL.",
      "sourceType": "org.springframework.boot.autoconfigure.thymeleaf.ThymeleafProperties",
      "defaultValue": ".html"
}

所以,项目中我只在application.properties中设置了cache属性

spring.thymeleaf.cache=false
3、编写模板文件

这里主要是html和vm文件的语法,简单的helloworld示例如下

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>hello world</title>
</head>
<body>

<h2> <span th:text="'hello world:' + ${name}"/></h2>

</body>
</html>
4、编写controller
@GetMapping("/{name}")
    public String helloWorld(@PathVariable(value = "name") String name, Model model) {
        // 设置模板文件中使用的参数
        model.addAttribute("name", name);
        // 返回模板文件名词,spring会指向 spring.thymeleaf.prefix + view name + spring.thymeleaf.suffix
        // 此处即为,classpath:templates/hellworld.html
        return "helloworld";
 }
5、测试
image-20210418000047037

最后,还是想说一下。个人感觉,服务端同学掌握一定的前端知识是有必要的,但是如果要求比较高,还是专业的人干专业的事吧。

相关文章

  • spring-boot-thymeleaf简单demo

    尽管现在提倡前后端分离,但是对于一些面向后端的应用系统,由于缺少前端人力,只能自己搭建。所以,SpringBoot...

  • Axure8小练习-简单的微信Demo(Round 1)

    这是一个很简单的Demo。 这是一个很简单的Demo。 这是一个很简单的Demo。 所以,随便瞅瞅吧。 小白,想做...

  • Electron开发 —— 简单demo之整合vue

    总得有个开始 前置条件: Electron开发 —— 简单demo vue开发 —— 简单demo vue 2.x...

  • Netty简单demo

    什么是Netty 以上是摘自《Essential Netty In Action》这本书,本文的内容也是本人读了这...

  • 简单的DEMO

    ViewController.m中 MyViewController.h中 MyViewController.m中...

  • Jedis简单Demo

    需要依赖的maven 简单案例 通过连接池连接的简单案例

  • TouchID简单demo

    基于swift3.0的TouchID简单应用 TouchID需要用到LocalAuthentication.fra...

  • GORM简单demo

    GORM和XORM一直是我关注着的两个Golang ORM框架,文档也是非常详细,,关于他们的比较详细地可以看文档...

  • NSPasteboard 简单Demo

    Youtube 演示视频: https://www.youtube.com/watch?v=-59238_PxgI...

  • Shiro简单demo

    参考Blog :http://wiki.jikexueyuan.com/project/shiro/authent...

网友评论

      本文标题:spring-boot-thymeleaf简单demo

      本文链接:https://www.haomeiwen.com/subject/tikmlltx.html