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

ops/2024/10/11 13:26:26/

资源下载

实验报告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/ops/117548.html

相关文章

计算机毕业设计 校园新闻管理系统的设计与实现 Java实战项目 附源码+文档+视频讲解

博主介绍&#xff1a;✌从事软件开发10年之余&#xff0c;专注于Java技术领域、Python人工智能及数据挖掘、小程序项目开发和Android项目开发等。CSDN、掘金、华为云、InfoQ、阿里云等平台优质作者✌ &#x1f345;文末获取源码联系&#x1f345; &#x1f447;&#x1f3fb; 精…

极狐GitLab 17.4 重点功能解读【四】

GitLab 是一个全球知名的一体化 DevOps 平台&#xff0c;很多人都通过私有化部署 GitLab 来进行源代码托管。极狐GitLab 是 GitLab 在中国的发行版&#xff0c;专门为中国程序员服务。可以一键式部署极狐GitLab。 学习极狐GitLab 的相关资料&#xff1a; 极狐GitLab 官网极狐…

mysql-connector-java本地试验

mysql-connector-java本地试验 题外话&#xff1a;因为Intellij配置报错java: 错误: 不支持发行版本 5&#xff0c;试过调整project structure也不行&#xff0c;所以直接在Ubuntu里搞 环境&#xff1a;Ubuntu 22.04软件&#xff1a;MySQL 8.0.39, jdk17.0.12 编写数据库 CR…

Spring Boot技术:构建高效网上购物平台

第3章 系统分析 3.1 可行性分析 在系统开发之初要进行系统可行分析&#xff0c;这样做的目的就是使用最小成本解决最大问题&#xff0c;一旦程序开发满足用户需要&#xff0c;带来的好处也是很多的。下面我们将从技术上、操作上、经济上等方面来考虑这个系统到底值不值得开发。…

C++游戏

宠粉福利&#xff01; 目录 1.猜数字 2.五子棋 3.打怪 4.跑酷 5.打飞机 6.扫雷 1.猜数字 #include <iostream> #include <cstdlib> #include <ctime>int main() {std::srand(static_cast<unsigned int>(std::time(0))); // 设置随机数种子int …

C++ 基于 Epoll 的多客户端聊天室项目

项目概述 这个项目实现了一个简单的 多客户端聊天室&#xff0c;基于 C 编程语言&#xff0c;使用了 epoll 机制来管理多个客户端连接。项目分为客户端和服务器端两部分&#xff0c;客户端通过 socket 连接到服务器&#xff0c;服务器可以处理多个客户端的消息并进行广播。 服…

Ego微商小程序项目实战4.0【环境搭建】

✨博客主页&#xff1a; https://blog.csdn.net/m0_63815035?typeblog &#x1f497;《博客内容》&#xff1a;.NET、Java.测试开发、Python、Android、Go、Node、Android前端小程序等相关领域知识 &#x1f4e2;博客专栏&#xff1a; https://blog.csdn.net/m0_63815035/cat…

五星级可视化页面(29):此次分享的界面,超越五星级啦

作为设计师&#xff0c;可以从超高颜值的可视化大屏界面上学习和借鉴以下几点&#xff1a; 1. 色彩运用&#xff1a; 观察颜值高的可视化大屏界面所运用的色彩搭配和调性&#xff0c;学习如何运用色彩来增强界面的视觉吸引力和信息传达效果。 2. 布局设计&#xff1a; 研究界…