IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> Java知识库 -> SpringBoot集成Thymeleaf -> 正文阅读

[Java知识库]SpringBoot集成Thymeleaf

环境

springboot2.4.3?+ thymeleaf2.4.3?+ bootstrap3

需求

浏览器中访问:http://localhost:8080/employee/list 列表员工数据

操作步骤

步骤1:创建项目 springboot-thymeleaf

?

步骤2:在/项目/resourcess/static 文件中导入bootstrap3相关文件

步骤3:导入依赖

核心依赖有3个:springboot parent? + web环境 + thymeleaf 依赖

在pom.xml文件中导入

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.3</version>
        <relativePath/>
    </parent>


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

步骤4:在/项目/resources/application.properties配置thymeleaf

#开启thymeleaf视图解析
spring.thymeleaf.enabled=true
#编码
spring.thymeleaf.encoding=utf-8
#前缀
spring.thymeleaf.prefix=classpath:/templates/
#是否使用缓存
spring.thymeleaf.cache=false
#严格的HTML语法模式
spring.thymeleaf.mode=HTML
#后缀名
spring.thymeleaf.suffix=.html

步骤5:编写相关相关代码

代码结构:

实体类:Employee

package com.langfeiyes.thymeleaf.entities;

/**
 * 员工对象
 */
public class Employee {
    private Long id;
    private String name;
    private int age;
    public Employee() {
    }
    public Employee(Long id, String name, int age) {
        this.id = id;
        this.name = name;
        this.age = age;
    }
    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
}

接口类:EmployeeController

package com.langfeiyes.thymeleaf.controller;


import com.langfeiyes.thymeleaf.entities.Employee;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

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

@Controller
@RequestMapping("employee")
public class EmployeeController {


    //假装是从数据库中查询出来的
    private List<Employee> queryList(){
        List<Employee> list = new ArrayList<>();
        list.add(new Employee(1L, "zhangsan", 16));
        list.add(new Employee(2L, "lisi", 17));
        list.add(new Employee(3L, "wangwu", 18));
        list.add(new Employee(4L, "zhaoliu", 19));
        list.add(new Employee(5L, "qianqi", 20));
        return list;
    }

    /**
     * 员工列表
     * @return
     */
    @RequestMapping("/list")
    public String list(Model model){
        model.addAttribute("emps", queryList());
        return  "employee/list"; //返回员工模板
    }

}

启动类:App

package com.langfeiyes.thymeleaf;


import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class App {
    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }
}

步骤6:编写员工列表模板

代码结构:

<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
    <title>员工列表</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="/bootstrap/css/bootstrap.css" type="text/css" />
    <script type="text/javascript" src="/bootstrap/js/bootstrap.js"></script>
</head>
<body>

<div class="container-fluid " style="margin-top: 20px">
    <div class="row">
        <div class="col-sm-10">
            <table class="table table-striped table-hover" >
                <thead>
                    <tr>
                        <th>序号</th>
                        <th>员工名称</th>
                        <th>员工年龄</th>
                        <th>操作</th>
                    </tr>
                </thead>
               <tr th:each="e,eStat:${emps}">
                   <td th:text="${eStat.index+1}"></td>
                   <td th:text="${e.name}"></td>
                   <td th:text="${e.age}"></td>

                   <td>
                       <a class="btn btn-info btn-xs " href="javascript:;" >
                           <span class="glyphicon glyphicon-edit"></span> 编辑
                       </a>
                       <a href="javascript:;" class="btn btn-danger btn-xs ">
                           <span class="glyphicon glyphicon-trash"></span> 删除
                       </a>
                   </td>
               </tr>
            </table>

        </div>
    </div>
</div>

</body>
</html>

?

步骤7:测试

右键执行App类

浏览器输入:http://localhost:8080/employee/list

显示最终效果:

?到这,Springboot集成thymeleaf成功。

案例源码:传送门

  Java知识库 最新文章
计算距离春节还有多长时间
系统开发系列 之WebService(spring框架+ma
springBoot+Cache(自定义有效时间配置)
SpringBoot整合mybatis实现增删改查、分页查
spring教程
SpringBoot+Vue实现美食交流网站的设计与实
虚拟机内存结构以及虚拟机中销毁和新建对象
SpringMVC---原理
小李同学: Java如何按多个字段分组
打印票据--java
上一篇文章      下一篇文章      查看所有文章
加:2021-08-06 09:29:41  更:2021-08-06 09:30:29 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/12 14:35:42-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码