SpringBoot2 学习5集成Thymeleaf

整个项目文件结构

API 参考地址:https://www.e-learn.cn/thymeleaf/standard-dialects
在这里插入图片描述

POM

关键:

org.springframework.boot
spring-boot-starter-thymeleaf

<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>
  <parent>
    <groupId>zz</groupId>
    <artifactId>SpringBoot2</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <artifactId>SpringBootThymeleaf</artifactId>
  <dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		
		<!--目的:《可选》引入springboot 热启动,每次修改以后,会自动把改动加载,不需要重启服务了-->
        <dependency> <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
        </dependency>
        
         <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
		<dependency>
		    <groupId>mysql</groupId>
		    <artifactId>mysql-connector-java</artifactId>
		    <version>8.0.15</version>
		</dependency>
		
		<dependency>
		    <groupId>org.springframework.boot</groupId>
		    <artifactId>spring-boot-starter-thymeleaf</artifactId>
		</dependency>
	</dependencies>
  
<repositories>
        <repository>
            <id>alimaven</id>
            <name>aliyun maven</name>
            <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
        </repository>
    </repositories>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

Controller

package com.zz.controller;

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

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

import com.zz.entity.Account;


@Controller
public class IndexController {
	
	@RequestMapping("/account")
	public String index(Model m) {
		List<Account> list = new ArrayList<Account>();
		list.add(new Account("KangKang", "康康", "e10adc3949ba59abbe56e", "超级管理员", "17777777777"));
		list.add(new Account("Mike", "麦克", "e10adc3949ba59abbe56e", "管理员", "13444444444"));
		list.add(new Account("Jane","简","e10adc3949ba59abbe56e","运维人员","18666666666"));
		list.add(new Account("Maria", "玛利亚", "e10adc3949ba59abbe56e", "清算人员", "19999999999"));
        m.addAttribute("accountList",list);
        return "account";
	}
}

template

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>account</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <link rel="stylesheet" th:href="@{/css/style.css}" type="text/css">
</head>
<body>
    <table>
		<tr>
			<th>no</th>
			<th>account</th>
			<th>name</th>
			<th>password</th>
			<th>accountType</th>
			<th>tel</th>
		</tr>
		<tr th:each="list,stat : ${accountList}">
		   <td th:text="${stat.count}"></td>
		   <td th:text="${list.account}"></td>
		   <td th:text="${list.name}"></td>
		   <td th:text="${list.password}"></td>
		   <td th:text="${list.accountType}"></td>
		   <td th:text="${list.tel}"></td>
		</tr>
    </table>
</body>
</html>

测试效果

在这里插入图片描述

语法

在这里插入图片描述

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>demo1</title>
</head>
<body>

<h1>${...} : 变量表达式。  演示</h1>

<h3>1 取值,直接显示</h3>
<span >你好</span>

<span th:text="${helloname}"></span>

<h3>2 对象,显示</h3>

<span th:text="${a.name}"></span>

<h3>3 List,显示</h3>
遍历(迭代)的语法th:each="自定义的元素变量名称 : ${集合变量名称}":
状态变量的使用语法:th:each="自定义的元素变量名称, 自定义的状态变量名称 : ${集合变量名称}":
不管什么时候,Thymeleaf 始终会为每个th:each创建一个状态变量,
默认的状态变量名称就是自定义的元素变量名称后面加Stat字符串组成

<table border="1">
<tr>
<th>名字</th>
<th>电话</th>
</tr>

<tr th:each="c:${cs}">
 <td th:text="${c.name}"></td>
 <td th:text="${c.tel}"></td>
</tr>

</table>


</body>
</html>

controller

package com.zz.controller;

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

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

import com.zz.entity.Account;


@Controller
@RequestMapping("/thymeleaf")
public class DemoController {
	
	@RequestMapping("/demo1")
	public String to_demo1(Model m) {
		m.addAttribute("helloname", "jerry");
		Account account=new Account();
		account.setName("小明");
		m.addAttribute("a", account);
		
		Account account1=new Account();
		account1.setName("小明");
		account1.setTel("23424");
		Account account2=new Account();
		account2.setName("小红");
		account2.setTel("322");
		Account account3=new Account();
		account3.setName("小王");
		account3.setTel("111");
		List<Account> clist=new ArrayList();
		clist.add(account1);
		clist.add(account2);
		clist.add(account3);
		m.addAttribute("cs", clist);
		
        return "demo1";
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值