SpringBoot- Thymelea入门

一)使用Thymelea进行数据的展示

1.pom文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.sxt</groupId>
    <artifactId>03-spring-boot-tym</artifactId>
    <version>1.0-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.7.RELEASE</version>
        <relativePath/>
    </parent>

    <dependencies>
        <!-- springBoot 的启动器 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- springBoot 的thymeleaf组件 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
    </dependencies>
</project>

二)Thymelea

模板引擎
1、市面上主流的 Java 模板引擎有:JSP、Velocity、Freemarker、Thymeleaf

2、JSP 本质也是模板引擎,Spring Boot 官方推荐使用 “Thymeleaf”模板引擎

3、模板引擎原理图如下,模板引擎的作用都是将模板(页面)和数据进行整合然后输出显示,区别在于不同的模板使用不同的语法,如 JSP 的JSTL表达式,以及J SP 自己的表达式和语法,同理 Thymeleaf 也有自己的语法
Thymelea:就是一种模板渲染技术,它使用特定的语法对html进行渲染,也就是说我们可以直接使用html做为视图层技术

Thymelea的使用
1.引入Thymelea坐标
2.在src/main/resources目录创建templates目录(默认为templates名字)
3.Controller

package com.sxt.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.jws.WebParam;

/**
 * @project_name:hibernate02
 * @date:2019/8/22:17:49
 * @author:shinelon
 * @Describe:
 */
@Controller
public class IndexController {

    @RequestMapping("/show")
    public String show(Model model){
        model.addAttribute("msg", "thymelear入门案例");
        return "index";
    }
}

4.视图

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Thymelear入门案例</title>
</head>
<body>
<!--th:text 标签相当于输出-->
        <span th:text="hello"></span>
        <hr/>
<!--        取控制器传递的值的方式和el表达式类似-->
        <span th:text="${msg}"></span>
</body>
</html>

5.页面显示效果
低版本的对html标签的格式要求是非常严谨的,标签必须有开始和结束,如果出现标签语法的问题,则是需要添加结束标签或则提高Thymelea版本,高版本允许html出现不标准的语法格式。

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值