栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

基于Thymeleaf与Bootstrap的Web开发实例

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

基于Thymeleaf与Bootstrap的Web开发实例

基于Thymeleaf与Bootstrap的Web开发实例

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

1-创建Maven项目,并在pom.xml文件中添加相关依赖。



    4.0.0

    org.example
    Thymeleaf
    1.0-SNAPSHOT
    
        
        org.springframework.boot
        spring-boot-starter-parent
        2.1.4.RELEASE
    
    
    
        
        org.springframework.boot
        spring-boot-starter-web
    

    
        org.springframework.boot
        spring-boot-starter-thymeleaf
    
    


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 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对象的数据。




    
    Book
    
    



图书列表

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.运行结果如下:

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/356213.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号