当前位置:网站首页>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";
}
}
边栏推荐
- If the watermark according to how to realize the function
- 谷粒商城-day13-es和商品上架
- How to seize the new trend of NFT, yuan|universe|universe?
- Ansible installation and configuration
- YAML文件格式
- 源码构建LAMP环境-3
- Redis是如何轻松实现系统秒杀的?
- 如何通过开源数据库管理工具 DBeaver 连接 TDengine
- Software testing pen questions 1 (with answers)
- 搭建直播平台,使用node生成验证码图片,并进行验证
猜你喜欢

VS保存后Unity不刷新

【使用pyside2遇到的问题】This application failed to start because no Qt platform plugin could be initialized.

总数据量超万亿行,玉溪卷烟厂通过正确选择时序数据库轻松应对

博客主题美化第二弹

YAML文件格式

第十章 时序与延迟

Auto.js脚本程序打包
![[TypeScript] Deep Learning of TypeScript Classes (Part 1)](/img/47/34954f1e01b844816d74f3ac556f9b.png)
[TypeScript] Deep Learning of TypeScript Classes (Part 1)

Ruoyi integrates minio to realize distributed file storage

圆锥折射作为偏振计量工具的模拟
随机推荐
SSM integration steps (emphasis)
【STM32学习3】DMA基础操作
万物智联时代,悄然走入生活
JS Date 时间戳 getTune data.parse 倒计时小程序
源码构建LAMP环境-3
抽象工厂模式
Unity WallFxPack使用
kubernetes pod podsecurityPolicies(PSP)
30天啃透这份Framework 源码手册直接面进大厂
工业元宇宙的价值和发展
用于中文文本分类的中文停用词
The only way to go from a monthly salary of 10k to 30k: automated testing
你离「TDengine 开发者大会」只差一条 SQL 语句!
以赛促练-力扣第304场周赛反思(持续更新中)
hi!Don't look at how to SAO gas dye-in-the-wood in MySQL?
工厂模式理解了没有?
第十章 时序与延迟
圆锥折射作为偏振计量工具的模拟
SSM整合步骤(重点)
[c] Detailed explanation of operators (1)