springboot整合thymeleaf

目录

一、介绍

二、Thymeleaf 的特点

三、语法规则

四、springboot中集成Thymeleaf

五、使用

六、将数据存入页面并取出

   6.1 存入普通数据

        6.1.2 存入对象

        6.1.3 存入List

6.2 跳转另一个页面

6.3 判断

写在最后


一、介绍

        Thymeleaf 是新一代 Java 模板引擎,Thymeleaf 支持 HTML 原型,其文件后缀为“.html”,因此它可以直接被浏览器打开,此时浏览器会忽略未定义的 Thymeleaf 标签属性,展示 thymeleaf 模板的静态页面效果;当通过 Web 应用程序访问时,Thymeleaf 会动态地替换掉静态内容,使页面动态显示。

二、Thymeleaf 的特点

 

动静结合、开箱即用、多方言支持等

三、语法规则

在使用 Thymeleaf 之前,首先要在页面的 html 标签中声明名称空间

xmlns:th="http://www.thymeleaf.org"

Thymeleaf 语法分为以下 2 类:

  • 标准表达式语法

                变量表达式:${...}

                选择变量表达式:*{...}

                链接表达式:@{...}

                国际化表达式:#{...}

                片段引用表达式:~{...}等

  • th 属性

          th:id 、 th:text、 th:utext、 th:object、 th:each、  th:if 等等、用来替换原有的html属性。  

四、springboot中集成Thymeleaf

        1、导入依赖

        

        <dependency>
            <groupId>org.thymeleaf</groupId>
            <artifactId>thymeleaf-spring5</artifactId>
        </dependency>

        2、配置

spring:
  thymeleaf:
    mode: HTML
    cache: false

五、使用

  1、在resource目录下新建templates、存放页面资源

        index.html如下

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

2、在IndexController,新建一个方法,拦截Get请求,拦截路劲为"/index",如下

 代码:

@Controller
public class IndexController {

    @GetMapping("/index")
    public String index(){
        return "index";
    }

}

启动后访问改路径:http://localhost:8889/index

 如此、便是测试成功了

六、将数据存入页面并取出

        6.1 存入普通数据

 6.1.1 存入普通数据 msg

    @GetMapping("/index")
    public String index(Model model){
        model.addAttribute("msg","hello ,springboot");
        return "index";
    }

 取出msg

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

    <h1 th:text="${msg}"></h1>
    <h1 th:utext="${msg}"></h1>

</body>
</html>

访问页面后的结果

6.1.2 存入对象

6.1.2 存入对象

 @Controller
public class IndexController {

    @GetMapping("/index")
    public String index(Model model){
        IUser user = new IUser();
        user.setUserName("张三");
        user.setEmail("123456@qq.com");
        user.setPassWord("123");
        model.addAttribute("user",user);
    }

}

取出存入对象

<h1 th:utext="${user}"></h1>

测试

 取出存入对象的某个值

<div th:object="${user}">
    <h1 th:text="${user.userName}"></h1>
    <h1 th:text="${user.passWord}"></h1>
</div>

测试

6.1.3 存入List

6.1.3 存入List

    @GetMapping("/index")
    public String index(Model model){
//        model.addAttribute("msg","hello ,springboot");

        IUser user1 = new IUser();
        user1.setUserName("张三");
        user1.setEmail("123456@qq.com");
        user1.setPassWord("123");

        IUser user2 = new IUser();
        user2.setUserName("张四");
        user2.setEmail("123456@qq.com");
        user2.setPassWord("1234");

        IUser user3 = new IUser();
        user3.setUserName("张五");
        user3.setEmail("123456@qq.com");
        user3.setPassWord("12345");

      List<IUser> userList = new ArrayList<>();
      userList.add(user1);
      userList.add(user2);
      userList.add(user3);
      model.addAttribute("userList",userList);

        return "index";
    }

遍历List并取出1

<h1 th:each="user :${userList}" th:text="${user}" ></h1>

测试

 遍历List并取出2

<div th:each="user :${userList}" >
            <h1 th:text="${user}"></h1>
</div>

测试

 遍历List并取出3

<div th:each="user :${userList}" >
           [[${user}]]
</div>

测试:

 

遍历userList,得到user,取出user的某个属性的值,

直接通过以属性的方式获取即可,例如:

<div th:each="user :${userList}" >
           [[${user.userName}]]
</div>

 测试

6.2 跳转另一个页面

准备:

    
    @GetMapping("/getUser")
    @ResponseBody
    private String getUser(){
        return "getUserDate";
    }

模拟下,访问此接口,得到User的所有数据并转为Json格式

跳转链接:

<a th:href="@{/getUser}">Thymeleaf写法:查询用户</a>
<hr>
<a href="/getUser">原生写法:查询用户</a>

 两种方法否可以实现跳转:

6.3 判断

  @GetMapping("/index")
    public String index(Model model){
        int a =1;
        model.addAttribute("msg",a);

        return "index";
    }

将int a =1,存入model对象,页面取出后进行判断

<div th:if="${msg<1}">
    <h1>a<1</h1>
</div>

<div th:unless="${msg>2}">
    <h1>a<=2</h1>
</div>

 基于可以根据条件展示不同结果、按钮等等

### 如何在 Unity 中向 MySQL 数据库的 A 添加字段 B 并设置默认值0 要在 Unity 项目中实现这一目标,可以通过 SQL 查询来完成。具体来说,可以使用 `ALTER TABLE` 命令向现有的添加新的字段,并为其指定默认值。 以下是完整的解决方案: #### 使用 ALTER TABLE 添加字段并设置默认值 SQL 的 `ALTER TABLE` 语句允许修改现有结构。要向 A 新增一个名为 B 的字段,并将其默认值设为 0,可以执行如下 SQL 语句: ```sql ALTER TABLE A ADD COLUMN B INT DEFAULT 0; ``` 这条命令的作用是: - 在 A 中新增一个整数类型的字段 B[^3]。 - 设置字段 B 的默认值0。 #### 在 Unity 中连接 MySQL 数据库 为了从 Unity 脚本中发送上述 SQL 命令MySQL 数据库,需要借助第三方插件(如 MySql.Data 或 SqlSugar)。以下是一个基于 SqlSugar 的示例代码片段: ##### 示例代码 ```csharp using System; using SqlSugar; public class DatabaseManager : MonoBehaviour { private SqlSugarClient db; void Start() { InitializeDatabase(); AddNewFieldToTableA(); } /// <summary> /// 初始化数据库连接 /// </summary> private void InitializeDatabase() { string connectionString = "Server=your_server_address;Port=3306;Database=your_database_name;Uid=your_username;Pwd=your_password;"; db = new SqlSugarClient(new ConnectionConfig() { ConnectionString = connectionString, DbType = DbType.MySql, IsAutoCloseConnection = true }); } /// <summary> /// 向 A 添加字段 B,默认值0 /// </summary> private void AddNewFieldToTableA() { try { string sqlCommand = "ALTER TABLE A ADD COLUMN B INT DEFAULT 0"; int result = db.Ado.ExecuteCommand(sqlCommand); if (result >= 0) { Debug.Log("成功向 A 添加字段 B"); } else { Debug.LogError("未能成功执行 SQL 命令"); } } catch (Exception ex) { Debug.LogError($"发生错误: {ex.Message}"); } } } ``` 这段代码的功能说明如下: 1. **初始化数据库连接**:通过 `InitializeDatabase()` 方法配置数据库连接字符串,并实例化 `SqlSugarClient` 对象[^2]。 2. **执行 SQL 命令**:调用 `AddNewFieldToTableA()` 方法,利用 `db.Ado.ExecuteCommand()` 执行自定义 SQL 命令。 3. **异常处理**:捕获可能发生的异常,并记录日志以便调试。 #### 注意事项 - 确保已安装适用于 Unity 的 SqlSugar 插件或类似的 MySQL 驱动程序。 - 替换连接字符串中的占位符(如 `your_server_address` 和 `your_username`),使其匹配实际的数据库配置。 - 如果 A 已经存在同名字段,则会抛出错误;建议先验证字段是否存在再尝试添加。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值