Springboot-thymeleaf project example (March 15, 2023)


foreword

After studying the springboot-thymeleaf project example, I would like to share my learning results with you. If you have any questions, you can leave a message and correct them. (Then springboot-thymeleaf project structure is built)


提示:以下是本篇文章正文内容,下面案例可供参考

Project example

1. Local configuration

  1. Windows 10 system
  2. Understand IDEA 2018
  3. JDK 1.8
  4. apache-maven-2.7.9

2. Operation steps

1. Create a new entity class

Create an entity folder in src\main\java\com\rj\springbootdemo01, and then create a new Book entity class in the folder.
insert image description here

2. Import lombok module

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.24</version>
        </dependency>

Add the code to the pom.xml file, and click the refresh button on the upper right to automatically download the module.
insert image description here

3. Download the plugin

Click File in the upper left corner, then click settings, click Plugins on the left, search for Lombok in it, download it, apply it, and restart the software.
---

4. Add attributes

Add book attributes (bookId, bookName) in the Book.java file.

package com.rj.springbootdemo01.entity;

import lombok.Data;

@Data
public class Book {
    
    
    private Integer bookId;
    private String bookName;

    public Book(Integer bookId, String bookName) {
    
    
        this.bookId = bookId;
        this.bookName = bookName;
    }
}

insert image description here

5. Add code in IndexController.java

package com.rj.springbootdemo01.controller;

import com.rj.springbootdemo01.entity.Book;
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
public class IndexController {
    
    
    @RequestMapping("index")
    public String index(Model model){
    
    
        //根据Tymeleaf模板,默认将返回src/main/resources/templates/index.html
        List<Book> books = new ArrayList<>();
        books.add(new Book(1,"html"));
        books.add(new Book(2,"java"));
        books.add(new Book(3,"mysql"));
        model.addAttribute("books",books);
        model.addAttribute("name","无名");
        return "index";
    }
}

insert image description here

6. Write the page

Write the page in the index.html file.

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
你好,<span th:text="${name}"></span><table>
    <tr>
        <th>序号</th><th>书名</th>
    </tr>
    <tr th:each="book,bookStat:${books}">
        <td th:text="${bookStat.count}"></td>
        <td th:text="${book.bookName}"></td>
    </tr>
</table>
</body>
</html>

insert image description here

7. run

insert image description here
insert image description here
The URL is: http://localhost:8080/index

Summarize

The above is the content of today's study. This article only briefly introduces the springboot-thymeleaf project example. There are still many places that have not been explored. If you have any questions above, you can correct them. Thank you.

Guess you like

Origin blog.csdn.net/qq_52992084/article/details/129578977