Controller 和 视图层编写
1. BookController 类编写 , 方法一:查询全部书籍
@Controller
@RequestMapping("/book")
public class BookController { @Autowired @Qualifier("BookServiceImpl") private BookService bookService; @RequestMapping("/allBook") public String list(Model model) { List<Books> list = bookService.queryAllBook(); model.addAttribute("list", list); return "allBook"; } }
2. 编写首页 index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" %>
<!DOCTYPE HTML>
<html>
<head>
<title>首页</title>
<style type="text/css">
a {
text-decoration: none;
color: black;
font-size: 18px;
}
h3 {
width: 180px;
height: 38px;
margin: 100px auto;
text-align: center; line-height: 38px; background: deepskyblue; border-radius: 4px; } </style> </head> <body> <h3> <a href="${pageContext.request.contextPath}/book/allBook">点击进入列表 页</a> </h3> </body> </html>
3. 书籍列表页面 allbook.jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>书籍列表</title>
<meta name="viewport" content="width=device-width, initialscale=1.0">
<!-- 引入 Bootstrap -->
<link
href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css"
rel="stylesheet">
</head>
<body>
<div class="container">
<div class="row clearfix">
<div class="col-md-12 column">
<div class="page-header">
<h1>
<small>书籍列表 —— 显示所有书籍</small>
</h1>
</div>
</div>
</div>
<div class="row">
<div class="col-md-4 column">
<a class="btn btn-primary"
href="${pageContext.request.contextPath}/book/toAddBook">新增</a>
</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>
<th>操作</th>
</tr>
</thead>
<tbody>
<c:forEach var="book" items="${requestScope.get('list')}"> <tr> <td>${book.getBookID()}</td> <td>${book.getBookName()}</td> <td>${book.getBookCounts()}</td> <td>${book.getDetail()}</td> <td> <a href="${pageContext.request.contextPath}/book/toUpdateBook? id=${book.getBookID()}">更改</a> | <a href="${pageContext.request.contextPath}/book/del/${book.getBookID()}"> 删除</a> </td> </tr> </c:forEach> </tbody> </table> </div> </div> </div>
4. BookController 类编写 , 方法二:添加书籍
@RequestMapping("/toAddBook")
public String toAddPaper() {
return "addBook";
}
@RequestMapping("/addBook")
public String addPaper(Books books) {
System.out.println(books);
bookService.addBook(books);
return "redirect:/book/allBook";
}
5. 添加书籍页面:addBook.jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>新增书籍</title>
<meta name="viewport" content="width=device-width, initialscale=1.0">
<!-- 引入 Bootstrap -->
<link
href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css"
rel="stylesheet">
</head>
<body>
<div class="container">
<div class="row clearfix">
<div class="col-md-12 column">
<div class="page-header">
<h1>
<small>新增书籍</small>
</h1>
</div>
</div>
</div>
<form action="${pageContext.request.contextPath}/book/addBook"
method="post">
书籍名称:<input type="text" name="bookName"><br><br><br>
书籍数量:<input type="text" name="bookCounts"><br><br><br>
书籍详情:<input type="text" name="detail"><br><br><br>
<input type="submit" value="添加">
</form>
</div>
6. BookController 类编写 , 方法三:修改书籍
@RequestMapping("/toUpdateBook")
public String toUpdateBook(Model model, int id) {
Books books = bookService.queryBookById(id);
System.out.println(books);
model.addAttribute("book",books );
return "updateBook";
}
@RequestMapping("/updateBook")
public String updateBook(Model model, Books book) {
System.out.println(book);
bookService.updateBook(book);
Books books = bookService.queryBookById(book.getBookID());
model.addAttribute("books", books);
return "redirect:/book/allBook";
}
7. 修改书籍页面 updateBook.jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>修改信息</title>
<meta name="viewport" content="width=device-width, initialscale=1.0">
<!-- 引入 Bootstrap -->
<link
href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css"
rel="stylesheet">
</head>
<body>
<div class="container">
<div class="row clearfix">
<div class="col-md-12 column">
<div class="page-header">
<h1>
<small>修改信息</small>
</h1>
</div>
</div>
</div>
<form action="${pageContext.request.contextPath}/book/updateBook"
method="post">
<input type="hidden" name="bookID" value="${book.getBookID()}"/>
书籍名称:<input type="text" name="bookName"
value="${book.getBookName()}"/>
书籍数量:<input type="text" name="bookCounts"
value="${book.getBookCounts()}"/>
书籍详情:<input type="text" name="detail"
value="${book.getDetail() }"/>
<input type="submit" value="提交"/>
</form>
</div>
8. BookController 类编写 , 方法四:删除书籍
@RequestMapping("/del/{bookId}")
public String deleteBook(@PathVariable("bookId") int id) {
bookService.deleteBookById(id);
return "redirect:/book/allBook";
}
项目结构图
实现查询书籍功能
1. 前端页面增加一个输入框和查询按钮
<div class="col-md-4 column"></div>
<div class="col-md-4 column">
<form class="form-inline" action="/book/queryBook" method="post"
style="float: right">
<input type="text" name="queryBookName" class="form-control"
placeholder="输入查询书名" required>
<input type="submit" value="查询" class="btn btn-primary">
</form>
</div>
2. 编写查询的Controller
@RequestMapping("/queryBook")
public String queryBook(String queryBookName,Model model){
System.out.println("要查询的书籍:"+queryBookName);
//业务逻辑还没有写
return "allBook";
}
3. 由于底层没有实现,所以我们要将底层代码先搞定
4. Mapper接口
//根据id查询,返回一个Book
Books queryBookByName(String bookName);
5. Mapper.xml
<!--根据书名查询,返回一个Book-->
<select id="queryBookByName" resultType="Books">
select * from ssmbuild.books
where bookName = #{bookName}
</select>
6. Service接口
//根据id查询,返回一个Book
Books queryBookByName(String bookName);
7. Service实现类
public Books queryBookByName(String bookName) {
return bookMapper.queryBookByName(bookName);
}
8. 完善Controller
@RequestMapping("/queryBook")
public String queryBook(String queryBookName,Model model){
System.out.println("要查询的书籍:"+queryBookName);
Books books = bookService.queryBookByName(queryBookName);
List<Books> list = new ArrayList<Books>();
list.add(books);
model.addAttribute("list", list);
return "allBook";
}
9. 测试,查询功能OK!
10. 无聊优化!我们发现查询的东西不存在的时候,查出来的页面是空的,我们可以提高一下用户的体 验性!
1. 在前端添加一个展示全部书籍的按钮
2. 并在后台增加一些判断性的代码!
@RequestMapping("/queryBook")
public String queryBook(String queryBookName,Model model){
System.out.println("要查询的书籍:"+queryBookName);
//如果查询的数据存在空格,则优化
Books books =
bookService.queryBookByName(queryBookName.trim());
List<Books> list = new ArrayList<Books>();
list.add(books);
//如果没有查出来书籍,则返回全部书籍列表
if (books==null){
list = bookService.queryAllBook();
model.addAttribute("error", "没有找到本书!");
}
model.addAttribute("list", list);
return "allBook";
}
3. 将错误信息展示在前台页面!完整的查询栏代码
<div class="row">
<div class="col-md-4 column">
<a class="btn btn-primary"
href="${pageContext.request.contextPath}/book/toAddBook">新增</a>
<a class="btn btn-primary"
href="${pageContext.request.contextPath}/book/allBook">显示全部书籍
</a>
</div>
<div class="col-md-8 column">
<form class="form-inline" action="/book/queryBook"
method="post" style="float: right">
<span style="color:red;font-weight: bold">${error}
</span>
<input type="text" name="queryBookName" class="formcontrol" placeholder="输入查询书名" required>
<input type="submit" value="查询" class="btn btnprimary">
</form>
</div>
</div>
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/128235.html