当前位置:网站首页>ssm整合(三)Controller 和 视图层编写
ssm整合(三)Controller 和 视图层编写
2022-08-02 21:50:00 【橘子ꦿ.๓】
首页
index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>首页</title>
<!-- 引入 Bootstrap -->
<link href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
<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>
书籍列表页
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, initial-scale=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>
<a class="btn btn-primary" href="${pageContext.request.contextPath}/book/allBook">显示全部书籍</a>
</div>
<div class="col-md-4 column"></div>
<div class="col-md-4 column">
<form class="form-inline" action="${pageContext.request.contextPath}/book/queryBook" method="post"
style="float: right">
<span style="color:red;font-weight: bold">${error}
</span>
<input type="text" name="queryBookName" class="form-control"
placeholder="输入查询书名" required>
<input type="submit" value="查询" class="btn btn-primary">
</form>
</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="${list}">
<tr>
<td>${book.bookID}</td>
<td>${book.bookName}</td>
<td>${book.bookCounts}</td>
<td>${book.detail}</td>
<td>
<a href="${pageContext.request.contextPath}/book/toUpdateBook?id=${book.getBookID()}">更改</a> |
<a href="${pageContext.request.contextPath}/book/deleteBook/${book.getBookID()}"> 删除</a>
</td>
</tr>
</c:forEach>
</tbody>
</table>
</div>
</div>
</div>
</body>
</html>
添加书籍页面
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, initial-scale=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" required><br>
书籍数量:<input type="text" name="bookCounts" required>><br>
书籍详情:<input type="text" name="detail" required>><br>
<input type="submit" value="添加">
</form>
</div>
修改书籍页面
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, initial-scale=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>
Controller代码
package com.bdqn.controller;
import com.bdqn.pojo.Books;
import com.bdqn.service.BookService;
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 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> list=bookService.queryAllBook();
model.addAttribute("list",list);
return "allBook";
}
//跳转到增加书籍页面
@RequestMapping("/toAddBook")
public String toAddPaper(){
return "addBook";
}
//添加书籍的请求
@RequestMapping("/addBook")
public String addBook(Books books){
System.out.println("books = " + books);
bookService.addBooks(books);
return "redirect:/book/allBook";//重定向到我们的@RequestMapping("/allBook")请求;
}
//跳转到修改书籍的页面
@RequestMapping("/toUpdate")
public String toUpdatePaper(int id,Model model){
Books books=bookService.queryBookById(id);
model.addAttribute("QBook",books);
return "updateBook";
}
//修改书籍
@RequestMapping("/updateBook")
public String updateBook(Books books){
System.out.println("updateBook ==> " + books);
int i=bookService.updateBook(books);
if(i>0){
System.out.println("添加books成功"+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){
Books books=bookService.queryBookByName(queryBookName);
List<Books> list=new ArrayList<Books>();
list.add(books);
if(books==null){
list=bookService.queryAllBook();
model.addAttribute("error","未查到");
}
model.addAttribute("list",list);
return "allBook";
}
}
边栏推荐
- hi!Don't look at how to SAO gas dye-in-the-wood in MySQL?
- Command line startup FAQs and solutions
- 任务四 机器学习库Scikit-learn
- LeetCode 2360. 图中的最长环 基环树找环+时间戳
- 30天啃透这份Framework 源码手册直接面进大厂
- wallys/new product/WiFi6 MiniPCIe Module 2T2R 2×2.4GHz 2x5GHz MT7915 MT7975
- go 反射 reflect 包
- 了解 NFT 质押:Web3 中赚取被动收益的另一种方式
- AcWing 2983. 玩具
- [c] Detailed explanation of operators (1)
猜你喜欢
随机推荐
I interviewed a 985 graduate, and I will never forget the expression when answering the "performance tuning" question
go 序列化与反序列化
[TypeScript] Deep Learning of TypeScript Classes (Part 1)
手把手教你干掉if else
Summary of @Transactional transaction invocation and effective scenarios
group of people
七夕到了——属于程序员的浪漫
The only way to go from a monthly salary of 10k to 30k: automated testing
【Unity】Unity开发进阶(七)双刃剑:扩展方法
Command line startup FAQs and solutions
Broadcast platform, the use of the node generated captcha image, and validate
了解 NFT 质押:Web3 中赚取被动收益的另一种方式
博客主题美化第二弹
【STM32学习3】DMA基础操作
牛客刷题:手动实现数组filter方法
VS保存后Unity不刷新
Byte's internal technical map is amazing and practical
【使用pyside2遇到的问题】This application failed to start because no Qt platform plugin could be initialized.
js函数防抖和函数节流及其他使用场景
工业元宇宙的价值和发展