springboot学习之路三

本章练习springboot引入模板引擎Thymeleaf

这对于我来说是新知识,从一开始对Java模板引擎的一脸茫然,到查阅资料后的大致了解。网上有说Thymeleaf是jsp的大哥。
实现目标:搭建项目建立HTML页面,使用thymeleaf标签显示数据。

目录结构
在这里插入图片描述
properties配置

spring.thymeleaf.prefix=classpath:/templates/user/
spring.thymeleaf.suffix=.html

pom.xml加入

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
 </dependency>

controller

@RequestMapping("/test01")
    public String test01(Model model){

        People people=new People();
        people.setAge(12);
        people.setName("普京");
        People people2=new People();
        people2.setAge(122);
        people2.setName("普京2");
        List<People> list=new ArrayList<People>();
        list.add(people);
        list.add(people2);
        System.out.println("xianxian");
        model.addAttribute("li","lizhonghua");
        model.addAttribute("people",people);
        model.addAttribute("list", list);
        model.addAttribute("panduan","判断");
        return "helloThymeleaf";
    }

HTML页面

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>hello LiZhongHua</h1>
<br>
<input type="text" value="哈哈">
<label>白宫一把手</label>
<p th:text="哈哈力争"></p>

<table>
    <tr>
        <td>姓名</td>
        <td>年龄</td>
    </tr>
    <tr>
        <td th:text="${people.name}"></td>
        <td th:text="${people.age}"></td>
    </tr>
    <h1>------------------------------------------</h1>
    <tr th:each="each,list:${list}">
        <td th:text="${each.name}"></td>
        <td th:text="${each.age}"></td>
    </tr>
</table>
<h2>=================================================</h2>
<!-- 条件判断,if和unless 条件一致,情况不同显示不同的文本  -->
<span th:if="${panduan=='判断'}" th:text="判断了"></span>
<span th:unless="${panduan == '判断'}" th:text="ggggggggggggggg"></span>
<h2>==================================================</h2>
<!-- 字符串拼接,用|变量、文本| -->
<p th:text="|${panduan },welcome.|"></p>
</body>
</html

浏览器访问 http://localhost:8080/test01
在这里插入图片描述
个人认为:这个模板引擎对于现在的我来说用处不大,暂且接触不到。还要以jsp为主,多搞多锻炼

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值