查询书籍功能
编写Controller类
@Controller
@RequestMapping("/book")
public class BookController {
//controller调用service层
@Autowired
@Qualifier("BookServiceImpl")
private BookService bookService;
//查询全部的书籍,并且返回到书籍展示页面
@RequestMapping("/allBook")
public String list(Model model){
List<Books> list = bookService.queryAllBook();
model.addAttribute("list",list);
return "allBook";
}
}
编写前端文件:
从前端index.jsp
跳转到页面展示allBook.jsp:在该页面中书籍是存储在model中的list中,需要从中取出来。
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>书籍展示</title>
<!-- Bootstrap -->
<link rel="stylesheet"
href="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<div class="row clearfix">
<div class="col-md-12 colum">
<div class="page-header">
<h1>
<small>书籍列表————显示所有书籍</small>
</h1>
</div>
</div>
</div>
<div class="row clearfix">
<div class="col-md-12 column">
<table class="table table-hover table-striped">
<thead>
<tr>
<th>书籍编号</th>
<th>书籍名称</th>
<th>书籍数量</th>
<th>书籍详情</th>
</tr>
</thead>
<tbody>
<%--书籍信息是从model的list中遍历出来的--%>
<c:forEach var="book" items="${list}">
<tr>
<td>${book.bookID}</td>
<td>${book.bookName}</td>
<td>${book.bookCounts}</td>
<td>${book.detail}</td>
</tr>
</c:forEach>
</tbody>
</table>
</div>
</div>
</div>
</body>
</html>
配置tomcat:
导入lib文件:
结果:
添加书籍功能
在首页添加页面跳转到添加页面:
编写Controller类,跳转到addBook页面
编写addBook页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>增加书籍</title>
<!-- Bootstrap -->
<link rel="stylesheet"
href="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<div class="row clearfix">
<div class="col-md-12 colum">
<div class="page-header">
<h1>
<small>增加书籍</small>
</h1>
</div>
</div>
</div>
<%--提交该表单到addBook路径中,name属性值必须要和实体类中的属性一致,required防止提交空值--%>
<form action="${pageContext.request.contextPath}/book/addBook" method="post">
<div class="form-group">
<label >书籍名称</label>
<input type="text" name="bookName" class="form-control" required>
</div>
<div class="form-group">
<label >书籍数量</label>
<input type="text" name="bookCounts" class="form-control" required>
</div>
<div class="form-group">
<label >书籍描述</label>
<input type="text" name="detail" class="form-control" required>
</div>
<div class="form-group">
<input type="submit" class="form-control" value="添加" >
</div>
</form>
</div>
</body>
</html>
在页面提交的信息由controller接收:调用业务层进行curd后重定向到书籍列表展示页面
修改书籍功能
在书籍列表后面添加操作,可以修改或删除。
点击修改操作后提交路径到toUpdateBook下,并且传递id给Controller类进行数据的查询。
调用service层进行数据查询后,将查询到的数据封装到model对象中给到QBook渲染到updateBook页面。查询到的数据通过value属性渲染到前端。
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>修改书籍</title>
<link rel="stylesheet"
href="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<div class="row clearfix">
<div class="col-md-12 colum">
<div class="page-header">
<h1>
<small>修改书籍</small>
</h1>
</div>
</div>
</div>
<%--提交该表单到addBook路径中,name属性值必须要和实体类中的属性一致,required防止提交空值--%>
<form action="${pageContext.request.contextPath}/book/updateBook" method="post">
<%--修改申请时,注意传递书籍id--%>
<input type="hidden" name="bookID" value="${QBook.bookID}">
<div class="form-group">
<label >书籍名称</label>
<%--书籍名称是从model中的QBook中获取--%>
<input type="text" name="bookName" class="form-control" value="${QBook.bookName}" required>
</div>
<div class="form-group">
<label >书籍数量</label>
<input type="text" name="bookCounts" class="form-control" value="${QBook.bookCounts}" required>
</div>
<div class="form-group">
<label >书籍描述</label>
<input type="text" name="detail" class="form-control" value="${QBook.detail}" required>
</div>
<div class="form-group">
<input type="submit" class="form-control" value="修改" >
</div>
</form>
</div>
</body>
</html>
页面显示
书籍信息填写完成后,将所填写的信息提交到Controller类中进行处理。提交的数据是存储在name中,注意保证name属性值与book实体类的属性值一致。
删除书籍功能
在户籍列表增加删除操作。传递需要删除的书籍id
点击删除操作时通过deleteBook路径跳转到对应的Controller中进行数据的处理。
查询书籍功能
该查询功能在底层并无业务处理操作。现进行业务的增加以及前端的增加。
首先,在前端编写一个页面查询按钮。该页面在提交时通过路径queryBook提交到Controller类中进行数据处理。通过name属性值queryBookName进行书籍名称的模糊查询。
编写mapper类。
编写mapper.xml文件
编写service类
编写serviceImpl类重写queryBookByName方法
编写Controller类接收数据:在将数据传送到业务层进行数据的处理。最后将获得的数据信息通过model传送的前端进行展示。