SpringBoot集成Thymeleaf

SpringBoot集成Thymeleaf

项目环境

  • IDEA 2019.1
  • SpringBoot 2.1.5
  • Gradle 4.10

技术介绍

操作步骤

添加依赖

plugins {
    id 'org.springframework.boot' version '2.1.5.RELEASE'
    id 'java'
}

apply plugin: 'io.spring.dependency-management'

group = 'org.virtue'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'

repositories {
    mavenCentral()
}

dependencies {
    //因为添加了log4j2的依赖,所以需要从boot-starter中去掉日志依赖,不然会jar包冲突
    compile('org.springframework.boot:spring-boot-starter-web') {
        exclude group: 'org.springframework.boot', module: 'spring-boot-starter-logging'
    }
    //log4j2jar包
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-log4j2', version: '1.5.6.RELEASE'
    //模板引擎
    compile('org.springframework.boot:spring-boot-starter-thymeleaf') {
        exclude group: 'org.springframework.boot', module: 'spring-boot-starter-logging'
    }
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

配置文件

server:
  port: 8880
  logging:
    config: classpath:logback-spring.xml
    path: logs/
spring:
  # 模板配置
  thymeleaf:
    cache: false # 这个开发配置为false,避免改了模板还要重启服务器
    prefix: classpath:/static/
    suffix: .html              # 下面3个不做解释了,可以不配置
    encoding: UTF-8
    mode: LEGACYHTML5      # 模板的模式
  mvc:
    view:
      prefix: classpath:/   # 定位模板的目录
      suffix: .html     # 给返回的页面添加后缀名

测试代码

MVC模式,进入页面需要通过视图,及View进入,我们在这里配置一个Controller用于进入页面。

@Controller
public class IndexController {

    @RequestMapping(value = "/test", method = {RequestMethod.GET})
    public String index(){
        return "test";
    }
}

我们在resources/static下新建一个test.html页面。

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="UTF-8">
    <title>Thymeleaf</title>
</head>
<body>
test
</body>
</html>

启动项目,在浏览器输入地址http://127.0.0.1:8880/test,即可进入页面,如下图所示:
在这里插入图片描述
我们在IndexController.java中添加一些测试数据,如下:

@Controller
public class IndexController {

    @RequestMapping(value = "/test", method = {RequestMethod.GET})
    public String index(Model model){
        model.addAttribute("name","张三");
        return "test";
    }
}

模板页面如下:

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="UTF-8">
    <title>Thymeleaf</title>
</head>
<body>
<!--基本数据显示-->
<a th:text="${name}"></a>
<!--字符串拼接-->
<p th:text="'Hello!, ' + ${name} + '!'">3333</p>

</body>
</html>

其中必须添加xmlns:th="http://www.w3.org/1999/xhtml"才能使用th标签。

  • 下图是一些测试的结果,如果想看具体代码可以参考最后的github地址:
    在这里插入图片描述

在页面中加入样式

  • 上面是没有加样式的测试结果,下面加点样式,让页面好看起来。
  • 引入导入静态文件,结构如下图:
    在这里插入图片描述
  • application.yml中指定css等资源路径
server:
  port: 8880
  logging:
    config: classpath:logback-spring.xml
    path: logs/
spring:
  # 模板配置
  thymeleaf:
    cache: false # 这个开发配置为false,避免改了模板还要重启服务器
    prefix: classpath:/static/
    suffix: .html              # 下面3个不做解释了,可以不配置
    encoding: UTF-8
    mode: LEGACYHTML5      # 模板的模式
  mvc:
    view:
      prefix: classpath:/   # 定位模板的目录
      suffix: .html     # 给返回的页面添加后缀名
  resources:
    static-locations: classpath:/static/src/

  • 在html中引入资源文件
 <link rel="stylesheet" href="src/css/bootstrap-theme.css"/>
    <link rel="stylesheet" href="src/css/bootstrap.css"/>
    <script src="src/js/bootstrap.js"></script>
  • 编写代码,最后页面效果如下:
    在这里插入图片描述

本篇制作了个简单的使用介绍,过程中主要注意引入包不要冲突,详细教程请移步官网。

项目代码

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
## springboot整合thymeleaf ### 1. 导入起步依赖 ```xml org.springframework.boot spring-boot-starter-thymeleaf ``` ### 2. 更改引入版本 ```xml 3.0.2.RELEASE 2.1.1 ``` > 1. springboot自带的thymeleaf依赖为2.1.3版本,使用thymeleaf-layout-dialect版本为2以下版本。 > 2. 使用3或3以上的thymeleaf时,需要thymeleaf-layout-dialect的版本为2或以上。 > 3. 锁定thymeleaf版本时不能使用thymeleaf.version标签,会和springboot内部的依赖标签冲突。应当使用springboot-thymeleaf.version标签来锁定版本。 ### 3. 配置文件配置 ```properties spring.thymeleaf.prefix=classpath:/templates/ spring.thymeleaf.check-template-location=true spring.thymeleaf.suffix=.html spring.thymeleaf.encoding=UTF-8 spring.thymeleaf.content-type=text/html spring.thymeleaf.mode=HTML spring.thymeleaf.cache=false ``` > spring.thymeleaf.cache为缓存,需要热部署时,需要设置为false ## 语法 ### 1. 替换标签体内容 ```html 显示欢迎 显示欢迎 ``` ### 2. 替换属性 ```html 显示欢迎 ``` ### 3. 在表达式中访问属性域 ```html 访问属性域 访问请求域 方式一 访问请求域 方式二 访问Session域 访Session域 方式一 访问Application域 方式一 ``` ### 4. 解析url地址 ```html 解析URL地址,获取ContextPath的值 @{}是把ContextPath的值附加到指定的地址前 @{}是把ContextPath的值附加到指定的地址前 ``` ### 5. 直接执行表达式 ```html 直接执行表达式 无转义效果 : [[${attrRequestScope}]] 有转义效果 : [(${attrRequestScope})] ``` ### 6. 分支与迭代 #### 1. if 判断 ```html if判断字符串是否为空 <p th

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值