spring boot框架与thymeleaf模板引擎的整合

1 篇文章 0 订阅
1 篇文章 0 订阅

1.首先新建项目
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

项目这就创建好了,下面配置pom.xml文件

<modelVersion>4.0.0</modelVersion>
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.5.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>

<properties>
    <java.version>1.8</java.version>
</properties>

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

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

    <!-- ThymeLeaf 依赖 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>

</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

2.实现功能
在这里插入图片描述
student实体类;

public class Student {
private int id;
private String name;
private String num;
private int sex;

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getNum() {
    return num;
}

public void setNum(String num) {
    this.num = num;
}

public int getSex() {
    return sex;
}

public void setSex(int sex) {
    this.sex = sex;
}

public Student(int id, String name, String num, int sex) {
    this.id = id;
    this.name = name;
    this.num = num;
    this.sex = sex;
}


@Override
public String toString() {
    return "Student{" +
            "id=" + id +
            ", name='" + name + '\'' +
            ", num='" + num + '\'' +
            ", sex=" + sex +
            '}';
}

studentController类:

package com.example.demo.student.controller;
import com.example.demo.student.entity.Student;
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;
import java.util.Map;
@Controller
@RequestMapping("stu")
public class StudentController {

@RequestMapping
public String list(Model model)
{
    List<Student>  list = new ArrayList<>();


    list.add(new Student(1,"张三","20141",1));
    list.add(new Student(2,"李四","20142",0));
    list.add(new Student(3,"王五","20143",0));
    list.add(new Student(4,"还是","20144",1));
    model.addAttribute("studentList",list);
    return "stu";
}

启动文件:

package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoApplication {

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

html页面:

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" 
xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity4">

<head lang="en">
<meta charset="UTF-8" />
<title></title>
</head>
<body>


<table border="1">

<tr>
    <td>ID</td>
    <td>名字</td>
    <td>学号</td>
    <td>性别</td>
</tr>
<tr th:each="student:${studentList}">
    <td th:text="${student.id}"></td>
    <td th:text="${student.name}"></td>
    <td th:text="${student.num}"></td>
    <td th:text="${student.sex}==0?'女':'男'"></td>
</tr>
</table>
</body>
</html>

启动好了:(Springboot中自带tomcat,端口默认8080)
在这里插入图片描述
html页面显示:
在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值