Java之SpringBoot-Thymeleaf详情

  • Post category:http

Java之SpringBoot-Thymeleaf详情攻略

简介

Spring Boot是一种创建基于Spring的应用程序的快速方法,是Spring Framework的一种扩展,为开发人员提供了在几分钟内创建Spring基础应用程序的能力。Thymeleaf是一种现代服务器端Java模板引擎,用于Web和独立环境中的Web应用程序。本文将介绍如何使用Spring Boot和Thymeleaf进行Web应用程序开发。

步骤

第一步:创建Spring Boot项目

使用Spring Initializr创建新的Spring Boot项目。在创建新项目时,需要选择包含“Thymeleaf”依赖项。

第二步:添加依赖项

在“pom.xml”文件中添加以下依赖项:

<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> 

第三步:创建控制器

创建一个控制器类,例如“AppController”,并设置“@Controller”注释。添加一个名为“home”的方法,该方法返回视图名称,并添加“@RequestMapping”注释。

@Controller
public class AppController {

    @RequestMapping("/")
    public String home(Model model) {
        model.addAttribute("message", "Hello Thymeleaf!");
        return "home";
    }

}

第四步:创建视图

在src/main/resources/templates目录下创建一个名为“home.html”的HTML文件。在该文件中,使用Thymeleaf语法创建HTML页面,包括标题和当前时间。

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Spring Boot Thymeleaf Example</title>
</head>
<body>
    <h1 th:text="${message}"></h1>
    <p>The current date and time is: <span th:text="${#temporal.format(java.time.LocalDateTime.now(), 'dd/MM/yyyy hh:mm:ss')}"></span></p>
</body>
</html>

第五步:运行应用程序

使用Spring Boot Maven插件运行应用程序,或使用“java -jar”命令运行生成的jar文件。

示例1

假设现在需要在页面显示一个用户的名字和年龄,则可以按照以下的方法进行修改。

AppController.java中的home方法:

@RequestMapping("/")
public String home(Model model) {
    User user = new User("Tom", 20);
    model.addAttribute("user", user);
    return "home";
}

home.html文件中的修改:

<body>
    <h1>Welcome, <span th:text="${user.name}"></span>!</h1>
    <p>You are <span th:text="${user.age}"></span> years old.</p>
    <p>The current date and time is: <span th:text="${#temporal.format(java.time.LocalDateTime.now(), 'dd/MM/yyyy hh:mm:ss')}"></span></p>
</body>

示例2

假设现在需要在页面显示一个包含员工信息的表格,则可以按照以下的方法进行修改。

AppController.java中的home方法:

@RequestMapping("/")
public String home(Model model) {
    List<Employee> employees = new ArrayList<>();
    employees.add(new Employee(1, "Tom", "CEO"));
    employees.add(new Employee(2, "Tim", "CTO"));
    employees.add(new Employee(3, "Jack", "CFO"));
    model.addAttribute("employees", employees);
    return "home";
}

home.html文件中的修改:

<body>
    <table>
        <thead>
            <tr>
                <th>ID</th>
                <th>Name</th>
                <th>Title</th>
            </tr>
        </thead>
        <tbody>
            <tr th:each="employee : ${employees}">
                <td th:text="${employee.id}"></td>
                <td th:text="${employee.name}"></td>
                <td th:text="${employee.title}"></td>
            </tr>
        </tbody>
    </table>
    <p>The current date and time is: <span th:text="${#temporal.format(java.time.LocalDateTime.now(), 'dd/MM/yyyy hh:mm:ss')}"></p>
</body>

结论

本文介绍了使用Spring Boot和Thymeleaf构建Web应用程序的详细步骤,包括创建Spring Boot项目、添加依赖项、创建控制器和视图等。此外,还提供了两个示例来演示如何使用Thymeleaf将数据呈现为HTML页面。 希望本文对您有所帮助。