SpringBoot2 学习5集成Thymeleaf

整个项目文件结构

SpringBoot2 学习5集成Thymeleaf

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>

测试效果

SpringBoot2 学习5集成Thymeleaf

上一篇:息队列介绍和SpringBoot2.x整合RockketMQ、ActiveMQ


下一篇:玩转springboot2.x 通过自定义配置类整合Druid(mybatis版)