Thymeleaf3 (二)使用spring boot 创建 thymeleaf项目

创建一个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>

运行程序,在浏览器中访问:

转载于:https://my.oschina.net/u/4006362/blog/2254533

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值