springboot学习4.1 —— thymeleaf语法1

从b站学习springcloud,现在进行总结,该总结除去了视频中出现的小错误,对有些易错的地方进行了提醒
b站链接:https://www.bilibili.com/video/av55993157
资料链接:
https://pan.baidu.com/s/1o0Aju3IydKA15Vo1pP4z5w
提取码: 21ru

上一节链接:https://blog.csdn.net/qq_40893824/article/details/104692505
下一节链接:https://blog.csdn.net/qq_40893824/article/details/104779625

下面的内容总结:

实现细节:
依然在上一节的工程springboot中实现:
1.在controller/Index.html中添加代码:

    @GetMapping("/index2")
    public String index2(Map<String,String>map){
        map.put("name","张三");
        return "index";
    }

完整IndexHandler代码:

package com.southwind.controller;

import com.southwind.entity.Student;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/index")
public class IndexHandler {

    @GetMapping("/index")
    public String index(Model model){
        System.out.println("index...");
        List<Student> list = new ArrayList<>();
        list.add(new Student(1l,"张三",12));
        list.add(new Student(2l,"李四",22));
        list.add(new Student(3l,"王五",14));
        model.addAttribute("list",list);
        return "index";
    }

    @GetMapping("/index2")
    public String index2(Map<String,String>map){
        map.put("name","张三");
        return "index";
    }
}

2.在resources/templates/index.html中,把,< table></ table>部分注释掉,
在其后加入代码:

    <p th:text="${name}"></p>
    <p th:text="'学生姓名:' + ${name} + 2"></p>
    <p th:text="|学生姓名:,${name}|"></p>

完整index.html代码:

<!DOCTYPE html>
<html xmlns:th="http:www.thymeleaf.org"></html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <h1>hello world</h1>
    <!--<table>
        <tr>
            <th>学生ID</th>
            <th>学生姓名</th>
            <th>学生年龄</th>
        </tr>
        <tr th:each="student:${list}">&lt;!&ndash; 对应第二行html &ndash;&gt;
            <td th:text="${student.id}"></td>
            <td th:text="${student.name}"></td>
            <td th:text="${student.age}"></td>
        </tr>
    </table>-->
    <p th:text="${name}"></p>
    <p th:text="'学生姓名:' + ${name} + 2"></p>
    <p th:text="|学生姓名:,${name}|"></p>
</body>
</html>

3.在开启 启动类Application
进入 http://localhost:9090/index/index2

条件判断:
4.在IndexHandler.java中添加代码:

    @GetMapping("/if")
    public String index3(Map<String,boolean>map){
        map.put("flag",true);
        return "index";
    }

5.在resources/templates/index.html中将3行< p>注释掉,在其后添加代码:

	<p th:if="${flag == true}" th:text="if判断成立"></p>
    <p th:unless="${flag != true}" th:text="unless判断成立"></p>
    <!-- unless的意思是当该情况不成立时候才成立,即负负得正,则输出text中内容 -->

6.启动 启动类Application
进入 http://localhost:9090/index/i

循环:
7.就是controller/IndexHandler中的:

    @GetMapping("/index")
    public String index(Model model){
        System.out.println("index...");
        List<Student> list = new ArrayList<>();
        list.add(new Student(1l,"张三",12));
        list.add(new Student(2l,"李四",22));
        list.add(new Student(3l,"王五",14));
        model.addAttribute("list",list);
        return "index";
    }

和 修改 resources/templates/index.html中的
注意 stat.index 不是 stat:index

    <table>
        <tr>
            <th>index</th>
            <th>count</th>
            <th>学生ID</th>
            <th>学生姓名</th>
            <th>学生年龄</th>
        </tr>
        <tr th:each="student,stat:${list}" th:style="'background-color:'+@{${stat.odd}?'#F2F2F2'}"><!-- 对应第二行html -->
            <!-- stat.odd表示如果是奇数行就变色为 #F2F2F2 -->
            <td th:text="${stat.index}"></td>
            <td th:text="${stat.count}"></td>
            <td th:text="${student.id}"></td>
            <td th:text="${student.name}"></td>
            <td th:text="${student.age}"></td>
        </tr>
    </table>

stat 是状态变量,属性:

名词
属性
解释
index集合中元素的index(从0开始)
count集合中元素的count(从1开始)
size集合的大小
current当前迭代变量
even/odd当前迭代是否为偶数/奇数(从0开始)
first当前迭代元素是否是第1个
last当前迭代元素是否是最后1个

8.重启Application
进入 http://localhost:9090/index/index

url超链接
通过 @{…} 进行处理
9.在resources/templates中新建test.html
第2行引入

<html xmlns:th="http:www.thymeleaf.org"></html>

在< body></ body>中加入代码

    <h1>hello world</h1>
    <a th:href="@{http://www.baidu.com}">跳转</a>

10.在IndexHandler中加入代码

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

11.进入 http://localhost:9090/index/test

12.在templates/test.html的< /a>后面加入

<a th:href="@{http://localhost:9090/index/url/{na}(na=${name})}">跳转2</a>

13.在 IndexHandler中修改test部分代码 并添加代码:

    @GetMapping("/test")
    public String test(Model model){
        model.addAttribute("name","tom");
        return "test";
    }

    @GetMapping("/url/{name}")
    @ResponseBody
    public String url(@PathVariable("name")String name){
        return name;
    }

14.重启 启动类Application
进入 http://localhost:9090/index/test


上一节链接:https://blog.csdn.net/qq_40893824/article/details/104692505
下一节链接:https://blog.csdn.net/qq_40893824/article/details/104779625

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

qq_1403034144

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值