实验报告2-前端框架和模板引擎实现视图

news/2024/9/29 0:57:34/

资源下载

实验报告2-前端框架和模板引擎实现视图

一、实现思路

        Spring Boot整合Thymeleaf实现图书管理案例。要求:

        1、项目使用Spring Boot整合Thymeleaf,项目展示的页面效果全部通过Thymeleaf的模板文件实现。

        2、查询所有图书。访问http://localhost:8080/book/list时,查询所有图书,并展示在页面中。

        3、选择性显示按钮。当Session中存在用户角色为“admin”时,显示“新增”按钮,否则不显示该按钮。

        4、按条件查询图书。单击“查询”按钮时,根据搜索框中的查询条件查询对应的图书信息。

        5、借阅图书。当图书状态为可借阅时,对应的“借阅”按钮为可用状态,并且单击“借阅”按钮时,将当前申请借阅图书的编号发送到后台。

二、实验步骤

1、新建项目

        Name:Exp2,GroupID:com.sw

        引入pom.xml文件、定义SpringBoots入口函数

2、测试Html静态页面

        com.sw.controller包,BookController.java

@Controller
@RequestMapping("/book")
public class BookController {@RequestMapping("/list")public ModelAndView list(){ModelAndView modelAndView = new ModelAndView("list");return modelAndView;}
}

        resources/static目录,导入css和js文件

        resources/templates目录,导入list.html

    <link rel="stylesheet" th:href="@{/css/bootstrap.css}"><script th:src="@{/js/jquery.min.js}"></script>

3、实现功能

(1)选择性显示按钮

        选择性显示按钮。当Session中存在用户角色为“admin”时,显示“新增”按钮,否则不显示该按钮。

        com.sw.pojo包,User.java

@Data
public class User {private String username;private String role;public User(String username, String role) {this.username = username;this.role = role;}
}

        com.sw.controller包,BookController.java

    @RequestMapping("/list")public ModelAndView list(String role, HttpSession session){ModelAndView modelAndView = new ModelAndView("list");session.removeAttribute("user");if (!ObjectUtils.isEmpty(role)){session.setAttribute("user",new User("zhangsan",role));}return modelAndView;}

        resources/templates目录,list.html

<div class="pull-left" th:if="${#session.getAttribute('user')!=null and #session.getAttribute('user').role=='admin'}"><div class="btn-group"><button type="button" class="btn btn-default">新增</button></div>
</div>
(2)查询所有图书

        访问http://localhost:8080/book/list时,查询所有图书,并展示在页面中。

        com.sw.pojo包,Book.java

@Data
public class Book {private Integer id;private String name;private String author;private String press;private Date publishDate;private BigDecimal price;private Integer status;
}

        com.sw.pojo包,Database.java

public class Database {public static List<Book> getBookList(){List<Book> bookList = new ArrayList<>();Book book1 = new Book();book1.setPrice(new BigDecimal(66));Calendar calendar = Calendar.getInstance();calendar.set(2021,9,10);book1.setPublishDate(calendar.getTime());bookList.add(book1);bookList.add(new Book());bookList.add(new Book());return bookList;}
}

        com.sw.controller包,BookController.java

        modelAndView.addObject("bookList", Database.getBookList());

        resources/templates目录,list.html

<tr th:each="book : ${bookList}"><td th:text="${book.name}"></td><td th:text="${book.author}"></td><td th:text="${book.press}"></td><td th:text="${#dates.format(book.publishDate,'yyyy-MM-dd')}"></td><td th:text="${#numbers.formatCurrency(book.price)}"></td><td><th:block th:if="${book.status==1}">可借阅</th:block><th:block th:if="${book.status==2}">借阅中</th:block><th:block th:if="${book.status==3}">归还中</th:block></td><td class="text-center"><button th:if="${book.status==1}" type="button" class="btn btn-primary btn-xs">借阅</button><button th:if="${book.status==2 || book.status==3}" type="button" class="btn btn-primary btn-xs" disabled="disabled">借阅</button></td>
</tr>
(3)按条件查询图书

        单击“查询”按钮时,根据搜索框中的查询条件查询对应的图书信息。

        resources/templates目录,list.html

<form method="post" action="/book/search">图书名称:<input name="name">图书作者:<input name="author"><input class="btn btn-default" type="submit" value="查询">
</form>

        com.sw.controller包,BookController.java

    @RequestMapping("/search")public ModelAndView search(Book bookFront){ModelAndView modelAndView = new ModelAndView("list");List<Book> bookList = new ArrayList<>();if (ObjectUtils.isEmpty(bookFront) ||(!ObjectUtils.isEmpty(bookFront)&& ObjectUtils.isEmpty(bookFront.getName())&& ObjectUtils.isEmpty(bookFront.getAuthor()))){bookList = Database.getBookList();}else {for (Book book : Database.getBookList()) {if (!ObjectUtils.isEmpty(bookFront.getName()) && !ObjectUtils.isEmpty(bookFront.getAuthor())){if (book.getName().contains(bookFront.getName()) && book.getAuthor().contains(bookFront.getAuthor())){bookList.add(book);}}else if(!ObjectUtils.isEmpty(bookFront.getName())){if (book.getName().contains(bookFront.getName())){bookList.add(book);}}else if(!ObjectUtils.isEmpty(bookFront.getAuthor())){if (book.getAuthor().contains(bookFront.getAuthor())){bookList.add(book);}}}}modelAndView.addObject("bookList", bookList);return modelAndView;}
(4)借阅图书

        当图书状态为可借阅时,对应的“借阅”按钮为可用状态,并且单击“借阅”按钮时,将当前申请借阅图书的编号发送到后台。

        resources/templates目录,list.html

<button th:if="${book.status==1}" type="button" class="btn btn-primary btn-xs" th:onclick="findBookById([[${book.id}]])">借阅</button>

com.sw.controller包,BookController.java

    @RequestMapping("/find/{id}")public ModelAndView find(@PathVariable Integer id){ModelAndView modelAndView = new ModelAndView("list");for (Book book : Database.getBookList()) {if (book.id == id){bookList.add(book);}}modelAndView.addObject("bookList", bookList);return modelAndView;}

        resources/templates目录,list.html

    function findBookById(id) {window.location = "/book/find/" + id// $.get("/book/find/" + id)// alert("findBookById:" + id)}


http://www.ppmy.cn/news/1531161.html

相关文章

基于SSM+小程序的儿童预防接种预约管理系统(疫苗1)(源码+sql脚本+视频导入教程+文档)

&#x1f449;文末查看项目功能视频演示获取源码sql脚本视频导入教程视频 1、项目介绍 本儿童预防接种预约微信小程序可以实现管理员和用户。 1、管理员功能有个人中心&#xff0c;用户管理&#xff0c;儿童信息管理&#xff0c;疫苗信息管理&#xff0c;儿童接种管理&#x…

Linux 文件 IO 管理(第三讲:文件系统)

Linux 文件 IO 管理&#xff08;第三讲&#xff1a;文件系统&#xff09; 进程为什么默认要打开文件描述符为 0&#xff0c;1 和 2 的文件呢&#xff1f;文件系统物理磁盘简单认识存储结构对磁盘存储进行逻辑抽象分组 —— 文件系统Block Bitmapinode Tableinode BitmapGDT(Gro…

MySql Explain优化命令使用

MySql Explain优化命令使用 truncate table student // 自增id 从 0 开始 delete from student // 自增id 会保留 &#xff0c; 108 区别&#xff1a; 1&#xff1a;自增id 2&#xff1a;delete 可以恢复 truncate 无法恢复 前言 EXPLAIN 是一个用于获取 SQL 语句执行计划的…

SpringCloud 2023 Gateway的Predicate配置详解、自定义Route Predicate Factory

目录 1. Predicate Factories介绍2. 常用的内置Route Predicate使用2.1 配置语法说明2.2 配置使用 3. 自定义Route Predicate Factory3.1 实现步骤&#xff1a;3.2 实现代码如下&#xff1a;3.3 application.yml配置3.4 测试 1. Predicate Factories介绍 Spring Cloud Gateway…

智能PPT行业赋能用户画像

智能PPT市场在巨大的需求前景下&#xff0c;已吸引一批不同类型的玩家投入参与竞争。从参与玩家类型来看&#xff0c;不乏各类与PPT创作有关的上下游企业逐步向智能PPT赛道转型进入&#xff0c;也包括顺应生成式AI技术热潮所推出的创业企业玩家。当前&#xff0c;智能PPT赛道发…

打造同城O2O平台:外卖跑腿APP的架构与功能设计详解

今天&#xff0c;小编将于大家共同讨论外卖跑腿APP的架构设计及其核心功能&#xff0c;旨在为开发者提供一份详尽的参考。 一、外卖跑腿APP的架构设计 1.整体架构概述 通常包括前端、后端和数据库。 2.前端设计 用户端提供直观的界面&#xff0c;方便用户下单、查询订单状态…

error -- unsupported GNU version gcc later than 10 are not supported;(gcc、g++)

服务器跑dit时编译flash-atten以及pytorch的cuda版本检查出错&#xff0c;分别报错题目以及如下&#xff1a; 想了下是系统找不到编译器 subprocess.CalledProcessError: Command [which, c] returned non-zero exit status 1. 备案&#xff0c;以后有人要用12我还得换回来 …

Lab1 Xv6 and Unix utilities

Lab1 Xv6 and Unix utilities 目的是为了熟悉xv6和一些它的系统调用函数 Boot xv6(easy) 1.环境 环境我是用的vscode配置的wsl&#xff0c;系统是ubuntu 20.04。用虚拟机、云服务器都感觉差不多。 网上看到Ubuntu 22.04 版本不适用于20年的课程&#xff0c;在根据20年课程…