创建一个spring boot 项目:
填入项目名称
选择web项目
选择模板引擎
结束
在开始介绍示例之前,我们先简单的分析⼀下示例程序的实体模型:客户(Customers)通过创建订单(Orders)来购买的产品(Products)。系统还提供了对产品评论(Comments)的管理:
Order订单关系
import lombok.Data;
import java.util.Date;
import java.util.Set;
@Data
public class Order {
private Integer id;
private Date date;
private Customer customer;
private Set<OrderLine> orderLines;
}
OrderLine订单
```java
import lombok.Data;
import java.math.BigDecimal;
[@Data](https://my.oschina.net/difrik)
public class OrderLine {
private Product product;
private Integer amount;
private BigDecimal purchasePrice;
}
Customer客户
import lombok.Data;
import java.util.Calendar;
[@Data](https://my.oschina.net/difrik)
public class Customer {
private Integer id ;
private String name;
private Calendar customerSince;
}
Product产品
import lombok.Data;
import java.math.BigDecimal;
import java.util.List;
[@Data](https://my.oschina.net/difrik)
public class Product {
private Integer id;
private String name;
private BigDecimal price;
private boolean inStock;
private List<Comments> comments;
}
Comments评论
import lombok.Data;
[@Data](https://my.oschina.net/difrik)
public class Comments {
private Integer id;
private String text;
}
ThymeleafApplication 启动类
package com.softtool.thymeleaf;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ThymeleafApplication {
public static void main(String[] args) {
SpringApplication.run(ThymeleafApplication.class, args);
}
}
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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>
<groupId>com.softtool</groupId>
<artifactId>thymeleaf</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>thymeleaf</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<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>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
示例程序模型
创建一个controller
package com.softtool.thymeleaf.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class IndexController {
@RequestMapping("/")
public String index(){
return "/index";
}
}
templates 目录下,创建index.html 页面
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Good Thymes Virtual Grocery</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<p >这是欢迎页面</p>
</body>
</html>
运行程序,在浏览器中访问: