当前位置:网站首页>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";
}
}
边栏推荐
- 同样月薪6K,为什么同事跳槽月薪翻倍,而你只涨了1000?
- 【学习笔记】博弈论
- 若依集成minio实现分布式文件存储
- 目前为止 DAO靠什么盈利?
- How does Redis easily achieve system instant kill?
- What is the core business model of the "advertising e-commerce" that has recently become popular in the circle of friends, and is the advertising revenue really reliable?
- CodeTON Round 2 A - D
- 牛客刷题:数组排序
- 源码构建LAMP环境-3
- iptables、firewalld的使用
猜你喜欢
PHP实现登录失败三次需要输入验证码需求
UDP(用户数据报协议)
Auto.js脚本程序打包
你我都会遇到的需求:如何导出MySQL中的数据~ 简单!实用!
LeetCode 2359. 找到离给定两个节点最近的节点 基环树
Command line startup FAQs and solutions
“百日行动”进行时:700余交通安全隐患被揪出
You and I will meet the needs of: how to export the data in a MySQL simple ~!Practical!
Sentinel vs Hystrix 限流对比,到底怎么选?
源码构建LAMP环境-2
随机推荐
用于中文文本分类的中文停用词
创建型模式 - 单例模式Singleton
面试了个985毕业的,回答“性能调优”题时表情令我毕生难忘
工业元宇宙的价值和发展
【Unity】Unity开发进阶(七)双刃剑:扩展方法
JS函数防抖&函数节流及其使用场景
刚安装完win10专业工作站版,系统变量中Path默认值有哪些?重新建一个“PATH”变量名,会覆盖掉原先的“Path”。
如何通过开源数据库管理工具 DBeaver 连接 TDengine
H5网页播放器EasyPlayer.js播放器界面的加载效果无法消失是什么原因?
谷粒商城-day14-商城业务与压力测试
Jmeter二次开发实现rsa加密
典型相关分析CCA计算过程
wallys/new product/WiFi6 MiniPCIe Module 2T2R 2×2.4GHz 2x5GHz MT7915 MT7975
创建型模式 - 抽象工厂模式AbstractFactory
【干货】分库分表最佳实践
CKA、CKAD、CKS、KCNA、CFCD考试
行业 SaaS 微服务稳定性保障实战
YAML文件格式
AcWing 2983. 玩具
【STM32学习3】DMA基础操作