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

资源下载

实验报告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

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

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

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

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

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

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

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

java 复制代码
        modelAndView.addObject("bookList", Database.getBookList());

resources/templates目录,list.html

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

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

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

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

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

javascript 复制代码
    function findBookById(id) {
        window.location = "/book/find/" + id
        // $.get("/book/find/" + id)
        // alert("findBookById:" + id)
    }
相关推荐
超级小的大杯柠檬水1 小时前
Spring Boot文件上传
java·spring boot·后端
code.song1 小时前
教师工作量|基于springBoot的教师工作量管理系统设计与实现(附项目源码+论文+数据库)
数据库·spring boot·后端
Mao.O1 小时前
在线聊天室项目(Vue3 + SpringBoot)
spring boot·websocket·vue3·在线聊天室
coder what1 小时前
基于springoot新能源充电系统的设计与实现
java·spring boot·后端·新能源充电系统
计算机学姐2 小时前
基于Hadoop的NBA球员大数据分析及可视化系统
java·大数据·vue.js·hadoop·spring boot·数据挖掘·数据分析
2401_857026233 小时前
Spring Boot框架在甘肃非遗文化网站设计中的运用
java·spring boot·后端
潘多编程3 小时前
Spring Boot 实战:使用观察者模式实现实时库存管理
spring boot·后端·观察者模式
计算机程序设计开发5 小时前
计算机毕业设计宠物领养网站我的发布领养领养用户信息/springboot/javaWEB/J2EE/MYSQL数据库/vue前后分离小程序
数据库·spring boot·java-ee·毕业设计·课程设计·计算机毕业设计·计算机毕业论文
fanhaifeng665 小时前
maven给springboot项目打成jar包 maven springboot打包配置
spring boot·maven·jar
山山而川粤5 小时前
宠物寄养系统小程序的设计
java·spring boot·后端·学习·小程序