掌握图书管理案例的实现,能够使用Spring Boot整合Thymeleaf完成图书管理案例。
1.任务需求
(1)项目使用Spring Boot整合Thymeleaf,项目展示的页面效果全部通过Thymeleaf的模板文件实现。
(2)查询所有图书。访问http://localhost:8080/book/list时,查询所有图书,并展示在页面中。
(3)选择性显示按钮。当Session中存在用户角色为“ADMIN”时,显示“新增”按钮,否则不显示该按钮。
(4)按条件查询图书。单击“查询”按钮时,根据搜索框中的查询条件查询对应的图书信息。
(5)借阅图书。当图书状态为可借阅时,对应的“借阅”按钮为可用状态,并且单击“借阅”按钮时,将当前申请借阅图书的编号异步发送到后台。
2.任务实现
(1)Spring Boot整合Thymeleaf
SpringBoot为Thymeleaf 提供了一系列默认配置,Spring Boot项目中只要引入Thymeleaf 的启动器,项目启动时就会自动进行对应配置,因此Spring Boot整合Thymeleaf只需在创建的Spring Boot项目中添加Thymeleaf 启动器即可。
(2)创建实体类
图书管理页面中包含2个实体对象,分别为用户实体和图书实体,并且当前还没有学习Spring Boot和持久层技术的整合,在此,选择通过一个类封装一些图书数据,页面展示的图书信息都从这个类中获取数据。需要创建用户类、图书类和数据类,具体代码如下所示。
import java.io.Serializable;
public class User implements Serializable {
private String name; //用户名称
private String role; //用户角色
public User(String name, String role) {
this .name = name;
this .role = role;
}
public String getName() {
return name;
}
public void setName(String name) {
this .name = name;
}
public String getRole() {
return role;
}
public void setRole(String role) {
this .role = role;
}
}
import java.io.Serializable;
public class Book {
private Integer id; //图书编号
private String name; //图书名称
private String author; //图书作者
private String press; //图书出版社
private String status; //图书状态
public Book(Integer id, String name, String author,String press, String status) {
this .id = id;
this .name = name;
this .press = press;
this .author = author;
this .status = status;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this .id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this .name = name;
}
public String getPress() {
return press;
}
public void setPress(String press) {
this .press = press;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this .author = author;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this .status = status;
}
@Override
public String toString() {
return "Book{" +
"id=" + id +
", name='" + name + ''' +
", author='" + author + ''' +
", press='" + press + ''' +
", status='" + status + ''' +
'
}';
}
}
import java.util.ArrayList;
public class Data {
public static ArrayList<Book> getData(){
ArrayList<Book> books=new ArrayList<>();
books.add(new Book(1,"楚辞","屈原","中国文联出版社","0"));
books.add(new Book(2,"纳兰词","纳兰性德","中国文联出版社","1"));
books.add(new Book(3,"西游记","吴承恩","中国文联出版社","2"));
return books;
}
}
(3)创建控制器类
在控制器类中创建对应的方法接收和响应页面发送的请求,具体代码如下所示。
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
@Controller
@RequestMapping("book")
public class BookController {
//获取所有图书信息
ArrayList<Book> books = Data.getData();
/**
* 查询所有图书
*/
@RequestMapping("list")
public String findBook(Model model, HttpSession session){
session.setAttribute("user",new User("zhangsan","ADMIN"));
model.addAttribute("books",books);
return "books";
}
/**
*按条件查询图书
*/
@RequestMapping("search")
public String searchBook(Book book,Model model,HttpSession session){
ArrayList<Book> bs=new ArrayList<>();
String bname=book.getName();
String bauthor=book.getAuthor();
if (bname.isEmpty()&&bauthor.isEmpty()){
bs=books;
}else {
for (Book b : books) {
if ((!bname.isEmpty()&&b.getName().contains(bname))||
(!bauthor.isEmpty()&&b.getAuthor().contains(bauthor))){
bs.add(b);
}
}
}
session.setAttribute("user",new User("zhangsan","ADMIN"));
model.addAttribute("books",bs);
return "books";
}
/**
*获取借阅图书的编号
*/
@RequestMapping("find/{id}")
public String findBook(@PathVariable("id") Integer id){
System.out.println("申请借阅图书的id:"+id);
return "books";
}
}
(4)创建模板文件
在resources\templates目录下创建名称为books的HTML文件作为模板文件,并将模板文件中通过Thymeleaf的属性和标准表达式引入样式文件,以及获取数据进行展示,具体代码如下所示。
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="utf-8">
<title>图书管理</title>
<link rel="stylesheet" th:href="@{/css/bootstrap.css}">
<link rel="stylesheet" th:href="@{/css/AdminLTE.css}">
<link rel="stylesheet" th:href="@{/css/pagination.css}">
<script th:src="@{/js/jquery.min.js}"></script>
</head>
<body>
<div class="box-body">
<div class="pull-left" th:if="${#session.getAttribute('user').role =='ADMIN'}">
<div class="btn-group">
<button type="button" class="btn btn-default"> 新增
</button>
</div>
</div>
<!--数据搜索 -->
<div class="pull-right">
<div class="has-feedback">
<form th:action="@{/book/search}" method="post">
图书名称:<input name="name">    
图书作者:<input name="author">    
<input class="btn btn-default" type="submit" value="查询">
</form>
</div>
</div>
<div class="table-box">
<!-- 数据表格 -->
<table id="dataList" class="table table-bordered table-striped table-hover text-center">
<thead>
<tr>
<th>图书名称</th>
<th>图书作者</th>
<th>出版社</th>
<th>图书状态</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<th:block th:each="book : ${books}">
<tr>
<td th:text="${book.name}"/>
</td>
<td th:text="${book.author}"></td>
<td th:text="${book.press}"></td>
<td>
<th:block th:if="${book.status == '0'}">
可借阅
</th:block>
<th:block th:if="${book.status =='1'}">
借阅中
</th:block>
<th:block th:if="${book.status =='2'}">
归还中
</th:block>
</td>
<td class="text-center">
<button th:if="${book.status =='0'}" type="button" class="btn bg-olive btn-xs"
th:onclick="findBookById([[${book.id}]]);"> 借阅
</button>
<button th:if="${book.status =='1' ||book.status =='2'}" type="button"
class="btn bg-olive btn-xs"
disabled="true">借阅
</button>
</td>
</tr>
</th:block>
</tbody>
</table>
</div>
</div>
</body>
<script>
function findBookById(id) {
$.get("/book/find/" + id)
}
</script>
</html>
(5)测试图书管理
启动项目,在浏览器中访问http://localhost:8080/book/list查询所有图书信息。
在搜索框中输入查询条件后单击“查询”按钮,按条件查询图书信息,以查询图书名称中包含“辞”字的图书信息为例。
单击图书《楚辞》的“借阅”按钮,此时IDEA控制台中输出图书对应的id。