SpringMVC-ssm案例-2023-04-23-2
Controller其他功能
package com.feijian.controller;
import com.feijian.pojo.Books;
import com.feijian.service.BookService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
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 org.springframework.web.bind.annotation.RequestParam;
import java.util.ArrayList;
import java.util.List;
@Controller
@RequestMapping("/book")
public class BookController {
//controller 调 service 层
@Autowired
@Qualifier("BookServiceImpl")
private BookService bookService;
//查询全部书籍,并返回一个书籍展示页面
@RequestMapping("/allBook")
public String list(Model model){
List<Books> booksList = bookService.queryAllBook();
model.addAttribute("list",booksList);
return "allBook";
}
//跳转到增加书籍页面
@RequestMapping("/toAddBook")
public String toAddPaper(){
return "addBook";
}
//添加书籍请求
@RequestMapping("/addBook")
public String addBook(Books books){
System.out.println("addBook ==> "+books);
bookService.addBook(books);
return "redirect:/book/allBook"; //重定向到@RequestMapping("allBook")请求中
}
//跳转到修改页面
@RequestMapping("/toUpdateBook/{bookID}")
public String toUpdatePaper(@PathVariable("bookID") int id, Model model){
Books book = bookService.queryBookById(id);
model.addAttribute("QBook",book);
return "updateBook";
}
//修改书籍
@RequestMapping("/updateBook")
public String updateBook(Books books){
System.out.println("updateBook ==> "+books);
bookService.updateBook(books);
return "redirect:/book/allBook";
}
//删除书籍
@RequestMapping("/deleteBook/{bookID}")
public String deleteBook(@PathVariable("bookID") int id){
bookService.deleteBookById(id);
return "redirect:/book/allBook";
}
//搜索书籍
@RequestMapping("/queryBook")
public String queryBook( String queryBookName, Model model){
List<Books> list = new ArrayList<Books>();
Books books = bookService.queryBookByName(queryBookName);
list.add(books);
//未搜索到返回全部书籍,并返回未查到错误提示
if(books == null){
list = bookService.queryAllBook();
model.addAttribute("error","未查到!");
}else {
model.addAttribute("list",list);
//return "allBook";
}
return "allBook";
}
}
addBook.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>添加书籍</title>
<%--BootStrap美化界面--%>
<!-- 引入 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">
<div class="form-group">
<label>书籍名称:</label>
<input type="text" class="form-control" name="bookName" required> <%--required:必须有内容才能提交--%>
</div>
<div class="form-group">
<label>书籍数量:</label>
<input type="text" class="form-control" name="bookCounts" required>
</div>
<div class="form-group">
<label>书籍描述:</label>
<input type="text" class="form-control" name="detail" required>
</div>
<div class="form-group">
<input type="submit" class="form-control" value="添加">
</div>
</form>
</div>
</body>
</html>
</body>
</html>
updateBook.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>修改书籍</title>
<%--BootStrap美化界面--%>
<!-- 引入 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="${QBook.bookID}" >
<div class="form-group">
<label>书籍名称:</label>
<input type="text" class="form-control" name="bookName" value="${QBook.bookName}" required> <%--required:必须有内容才能提交--%>
</div>
<div class="form-group">
<label>书籍数量:</label>
<input type="text" class="form-control" name="bookCounts" value="${QBook.bookCounts}" required>
</div>
<div class="form-group">
<label>书籍描述:</label>
<input type="text" class="form-control" name="detail" value="${QBook.detail}" required>
</div>
<div class="form-group">
<input type="submit" class="form-control" value="修改">
</div>
</form>
</div>
</body>
</html>