基于Thymeleaf与Bootstrap的Web开发实例

基于Thymeleaf与Bootstrap的Web开发实例

在进行Web开发中,尽量使用前端开发工具包BootStrap,jQuery和SpringMVC框架,下面学习一个基于Thymeleaf模板引擎的Spring Boot Web应用。

1-创建Maven项目,并在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>org.example</groupId>
    <artifactId>Thymeleaf</artifactId>
    <version>1.0-SNAPSHOT</version>
    <parent>
        <!--配置SpringBoot的核心启动器-->
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.4.RELEASE</version>
    </parent>
    <dependencies>
    <dependency>
        <!--添加starter模块-->
        <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>
    </dependencies>

</project>

2-在src/main/java目录下创建com.model包,在该包中创建实体模型类Book,在该类中定义书的信息,设置构造器,访问方法和修改方法。

public class Book {
    String isbn  ;
    Double price ;
    String bname ;
    String publishing ;
    String author ;
    String picture ;
    public Book(String isbn, Double price, String bname, String publishing, String author, String picture) {
        this.isbn = isbn;
        this.price = price;
        this.bname = bname;
        this.publishing = publishing;
        this.author = author;
        this.picture = picture;
    }

    public String getIsbn() {
        return isbn;
    }

    public void setIsbn(String isbn) {
        this.isbn = isbn;
    }

    public Double getPrice() {
        return price;
    }

    public void setPrice(Double price) {
        this.price = price;
    }

    public String getBname() {
        return bname;
    }

    public void setBname(String bname) {
        this.bname = bname;
    }

    public String getPublishing() {
        return publishing;
    }

    public void setPublishing(String publishing) {
        this.publishing = publishing;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public String getPicture() {
        return picture;
    }

    public void setPicture(String picture) {
        this.picture = picture;
    }
}

3-在src/main/java目录下创建包com.controller,在该包中创建控制器类ThymeleafController。在该控制器类中实例化Book类的多个对象,并保存到集合ArrayList< Book >中,并暴露为模型数据,供视图页面获取。

import com.model.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 ThymeleafController {
    @RequestMapping("/")
    public String index(Model model){
        Book book1 = new Book("1234", 56.0, "Java实用教程", "清华大学出版社", "耿老师", "1.jpg") ;
        List<Book> books = new ArrayList<>() ;
        Book book2 = new Book("1245", 55.0, "JavaWeb开发从入门到实战", "清华大学出版社", "陈老师", "2.png") ;
        books.add(book2) ;
        Book book3 = new Book("1236", 55.5, "JavaEE框架开发从入门到实践", "清华大学出版社", "陈老师", "3.jpg") ;
        books.add(book3) ;
        model.addAttribute("aBook", book1) ;
        model.addAttribute("bBook", books) ;
        //使用Thymeleaf模板,将默认返回src/main/resources/templates/index.html
        return "index" ;
    }
}

4-添加静态图片和引入Bootstrap框架。在src/main/resources目录创建static目录,在该目录下创建images目录和css目录,images目录中放入三张图片,css中引入bootstrap。
在这里插入图片描述

5-创建View视图页面。Tymeleadf模板默认将视图页面放在src/main/resources/templates目录下,因此在该目录下创建index.html视图页面,在该页面中使用Tymeleaf模板显示控制器类ThymeleafController中的model对象的数据。

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="UTF-8">
    <title>Book</title>
    <link rel = "stylesheet" th:href = "@{css/bootstrap.min.css}"/>
    <link rel = "stylesheet" th:href = "@{css/bootstrap-theme.min.css}"/>
</head>
<body>
<!--面板-->
<div class = "panel panel-primary">
    <!--面板头部信息-->
    <div class = "page-header">
        <!--面板标题-->
        <h3 class = "panel-title">第一个基于Thymeleaf的模板引擎的SpringBoot Web应用</h3>
    </div>
</div>
<!--容器-->
<div class = "container">
    <div>
        <h4>图书列表</h4>
    </div>
<div class = "row">
    <!--col-md针对桌面显示器,col-sm针对平板-->
    <div class="col-md-4 col-sm-6">
        <a href = "">
            <img th:src = "'images/' +${aBook.picture}" alt="图书封面" style="height: 180px; width: 40%"/>
        </a>
        <!--caption容器中放置其它基本信息,例如标题,文本描述等-->
        <div class = "caption">
            <h4 th:text = ${aBook.bname}></h4>
            <p th:text = ${aBook.author}></p>
            <p th:text = "${aBook.isbn}"></p>
            <p th:text = ${aBook.price}></p>
            <p th:text = ${aBook.publishing}></p>
        </div>
    </div>
    <!--循环取出数据集合-->
    <div class = "col-md-4 col-sm-6" th:each = "book:${bBook}">
        <a href = "">
            <img th:src = "'images/' + ${book.picture}" alt="图片封面" style = "height: 180px; width: 40%"/>
        </a>
        <div class = "caption">
            <h4 th:text = "${book.bname}"></h4>
            <p th:text = "${book.author}"></p>
            <p th:text = "${book.isbn}"></p>
            <p th:text = "${book.price}"></p>
            <p th:text = "${book.publishing}"></p>
        </div>
    </div>
</div>
</div>
</body>
</html>

7.在src/nain/java下创建包com.test,在该包中编写启动类,运行启动类,然后 ,访问http://localhost:8080/

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication(scanBasePackages = {"com"})
public class TestApplication {
    public static void main(String[] args) {
        SpringApplication.run(TestApplication.class, args) ;
    }
}

8.运行结果如下:
在这里插入图片描述

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
当前课程中商城项目的实战源码是我发布在 GitHub 上的开源项目 newbee-mall (新蜂商城),目前已有 9900 多个 Star,本课程是一个 Spring Boot 技术栈的实战类课程,课程共分为 3 大部分,前面两个部分为基础环境准备和相关概念介绍,第三个部分是 Spring Boot 商城项目功能的讲解,让大家实际操作并实践上手一个大型的线上商城项目,并学习到一定的开发经验以及其中的开发技巧。商城项目所涉及的功能结构图整理如下: 作者寄语本课程录制于2019年,距今已有一段时间。期间,Spring Boot技术栈也有一些版本升级,比如Spring Boot 2.7.x发版、Spring Boot 3.x版本正式版本。对于这些情况,笔者会在本课程实战项目的开源仓库中创建不同的代码分支,保持实战项目的源码更新,保证读者朋友们不会学习过气的知识点。新蜂商城的优化和迭代工作不会停止,不仅仅是功能的优化,在技术栈上也会不断的增加,截止2023年,新蜂商城已经发布了 7 个重要的版本,版本记录及开发计划如下图所示。 课程特色 对新手开发者十分友好,无需复杂的操作步骤,仅需 2 秒就可以启动这个完整的商城项目最终的实战项目是一个企业级别的 Spring Boot 大型项目,对于各个阶段的 Java 开发者都是极佳的选择实践项目页面美观且实用,交互效果完美教程详细开发教程详细完整、文档资源齐全代码+讲解+演示网站全方位保证,向 Hello World 教程说拜拜技术栈新颖且知识点丰富,学习后可以提升大家对于知识的理解和掌握,可以进一步提升你的市场竞争力 课程预览 以下为商城项目的页面和功能展示,分别为:商城首页 1商城首页 2购物车订单结算订单列表支付页面后台管理系统登录页商品管理商品编辑

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

nuist__NJUPT

给个鼓励吧,谢谢你

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值