整合SSM
环境要求
- IDEA
- Tomcat 9
- Maven3.6
- MySQL 8.0,25(根据自己需求而定)
一、数据库环境
创建数据库的SQL代码
-- 创建数据库
CREATE DATABASE `ssmTest`
USE `ssmTest`
DROP TABLE IF EXISTS `book`
CREATE TABLE `book`
(
`bookId` INT(10) NOT NULL AUTO_INCREMENT COMMENT '主键ID',
`bookName` VARCHAR(100) NOT NULL COMMENT '书籍名',
`bookCount` INT(11) NOT NULL COMMENT '数量',
`detail` VARCHAR(200) NOT NULL COMMENT'描述',
KEY `bookId`(`bookId`)
)ENGINE = INNODB DEFAULT CHARSET = utf8
INSERT INTO `book`(`bookId`,`bookName`,`bookCount`,`detail`)
VALUES(1,'Java',1,'从入门到放弃'),
(2,'Mysql',10,'从删库到跑路'),
(3,'Spring',5,'从开始到结束')
SELECT * FROM `book`
二、基本环境搭建
1.新建一个Maven项目SpringMVC-10-mybatis,添加web的支持
2.导入相关的pom依赖
<!-- 导入jar包依赖-->
<dependencies>
<!-- 测试包-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
<!-- lombok包-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.22</version>
</dependency>
<!-- Servlet的API-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
<!-- JSP的API-->
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.2</version>
</dependency>
<!-- jstl的API-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!-- mysql连接数据库的包-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.25</version>
</dependency>
<!-- 数据库连接池-->
<!-- https://mvnrepository.com/artifact/com.mchange/c3p0 -->
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.2</version>
</dependency>
<!-- mybatis的包-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.7</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.6</version>
</dependency>
<!-- Spring的包-->
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.13</version>
</dependency>
<!-- Spring的jdbc包-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.3.14</version>
</dependency>
<!-- Aop织入包-->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.7</version>
</dependency>
</dependencies>
3.、解决Maven资源过滤的问题
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>
4.在WEB-INF下加载所有的依赖
- 打开Project Structure,点击WEBINF,再点击含有文件夹的+图标,进行添加文件夹
-
WEB-INF下添加lib,点击+号图标,否则加载项目不出或者出现404错误。
特殊情况: -
运行项目时,出现找到多个名为spring_web的片段。这是不合法的相对排序。有关详细信
息,请参阅Servlet规范的第8.2.2 2c节的错误。
-
解决方案:项目lib中有重复的Spring框架,删除spring-web依赖即可.
5.建立基本结构和配置框架 -
com.Liang.pojo
-
com.Liang.mapper.
-
com.Liang.service
-
com.Liang.controller
-
mybatis-config.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
</configuration>
- application.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
</beans>
三、Mybatis层编写
1.数据库配置文件 db.properties
jdbc.driver = com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ssmTest?useSSL=true&characterEncoding=utf8&useUnicode=true&serverTimezone=Asia/Shanghai
jdbc.username=root
jdbc.password=root
2.IDEA关联数据库
3.编写Mybatis核心配置文件 mybatis-config.xml
<?xml version="1.0" encoding="gb2312" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<typeAliases>
<package name="com.Liang.pojo"/>
</typeAliases>
<!-- 配置设置-->
<!-- <settings>-->
<!-- <setting name="" value=""/>-->
<!-- </settings>-->
</configuration>
4.编写数据库对应的实体类 com.Liang.pojo.Books
使用lombok插件
package com.Liang.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Books {
private int bookId;
private String bookName;
private int bookCount;
private String detail;
public Books(String bookName,int bookCount,String detail)
{
this.bookName=bookName;
this.bookCount=bookCount;
this.detail=detail;
}
}
5.编写Dao层的Mapper接口 BookMapper
import com.Liang.pojo.Books;
import java.util.List;
public interface BookMapper {
//新增一个书籍
int addBook(Books book);
//删除指定书籍
int deleteBook(int id);
//更新书籍信息
int updateBook(Books books);
//通过id查询指定书籍
Books queryById(int id);
//通过BookName查询指定书籍
Books queryByBookName(String bookName);
//查询全部书籍
List<Books> queryAllBook();
}
6.编写接口对应的Mapper.xml BookMapper.xml
<?xml version="1.0" encoding="gb2312" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.Liang.mapper.BookMapper">
<!-- 新增书籍-->
<insert id="addBook" parameterType="Books">
insert into ssmTest.book(`bookName`,`bookCount`,`detail`)
values(#{bookName},#{bookCount},#{detail})
</insert>
<!-- 删除书籍-->
<delete id="deleteBook" parameterType="int">
delete from ssmTest.book where bookId = ${bookID}
</delete>
<!-- 更新书籍信息-->
<update id="updateBook" parameterType="Books">
update ssmTest.book set bookName = #{bookName},bookCount=#{bookCount},
detail =#{detail} where bookId = #{bookId}
</update>
<!-- 通过BookId查询指定书籍-->
<select id="queryById" parameterType="int" resultType="Books">
select * from ssmTest.book
where bookId = #{bookId}
</select>
<!-- 通过BookName查询指定书籍-->
<select id="queryByBookName" parameterType="String" resultType="Books">
select * from ssmTest.book
where bookName = #{bookName}
</select>
<!-- 查询全部书籍-->
<select id="queryAllBook" resultType="Books">
select * from ssmTest.book
</select>
</mapper>
7.编写Service接口和实现类
Service接口:
package com.Liang.service;
import com.Liang.mapper.BookMapper;
import com.Liang.pojo.Books;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.awt.print.Book;
import java.util.List;
@Service
public class BookServiceImpl implements BookService{
@Autowired
//调用底层mapper,并设置一个set接口,方便Spring容器统一管理
private BookMapper bookMapper;
public void setBookMapper(BookMapper bookMapper)
{
this.bookMapper=bookMapper;
}
@Override
public int addBook(Books book) {
return bookMapper.addBook(book);
}
@Override
public int deleteBook(int id) {
return bookMapper.deleteBook(id);
}
@Override
public int updateBook(Books books) {
return bookMapper.updateBook(books);
}
@Override
public Books queryById(int id) {
return bookMapper.queryById(id);
}
@Override
public Books queryByBookName(String bookName) {
return bookMapper.queryByBookName(bookName);
}
@Override
public List<Books> queryAllBook() {
return bookMapper.queryAllBook();
}
}
四、Spring层
1.编写Spring整合Mybatis相关的配置文件 spring-dao.xml<?xml version="1.0" encoding="gb2312"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 整合mybatis整合-->
<!-- 1.配置关联性数据库文件-->
<context:property-placeholder location="classpath:db.properties"/>
<!-- 2.配置数据库连接池-->
<!-- dbcp 半自动化操作 不能自动连接
c3po 自动化操作(自动的加载配置文件,并且设置在对象里面)-->
<bean class="com.mchange.v2.c3p0.ComboPooledDataSource" id="dataSource" destroy-method="close">
<!-- 配置连接池属性-->
<property name="driverClass" value="${jdbc.driver}"/>
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="user" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<!-- 配置连接池私有属性-->
<property name="maxPoolSize" value="30"/>
<property name="minPoolSize" value="10"/>
<!-- 关闭连接后不自动commit-->
<property name="autoCommitOnClose" value="false"/>
<!-- 获取连接超时时间-->
<property name="checkoutTimeout" value="10000"/>
<!-- 重试次数-->
<property name="acquireRetryAttempts" value="2"/>
</bean>
<!-- 3.配置SqlSessionFactory对象-->
<bean id="SqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 配置数据源-->
<property name="dataSource" ref="dataSource"/>
<!-- 配置mapper映射文件 -->
<property name="mapperLocations" value="classpath:BookMapper.xml"/>
<!-- 配置全局配置文件-->
<property name="configLocation" value="classpath:mybatis-config.xml"/>
</bean>
<!-- 4.配置扫描Mapper接口包,动态实现Mapper接口注入到Spring容器中,获取mapper接口代理-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 注入SqlSessionFactory-->
<property name="sqlSessionFactoryBeanName" value="SqlSessionFactory"/>
<!-- 给定Mapper接口包-->
<property name="basePackage" value="com.Liang.mapper"/>
</bean>
</beans>
新知识:动态实现Maper接口注入到Spring容器
MapperScannerConfigurer能帮助我们自动生成Mapper代理对象,需要注入Mapper接口包
过去
我们需要用到SqlSeesion去反射获得对应的Mapper对象,手动写入Mapper接口
现在
MapperScannerConfigurer , 它 将 会 查 找 类 路 径 下 的 映 射 器 并 自 动 将 它 们 创 建 成 MapperFactoryBean
配置Value的原因:
- 注 意 , 没 有 必 要 去 指 定 SqlSessionFactory 或 SqlSessionTemplate , 因 为 MapperScannerConfigurer 将会创建 MapperFactoryBean,之后自动装配。但是,如果你使 用了一个 以上的 DataSource ,那 么自动 装配可 能会失效 。这种 情况下 ,你可 以使用 sqlSessionFactoryBeanName 或 sqlSessionTemplateBeanName 属性来设置正确的 bean 名 称来使用。
引自:https://www.cnblogs.com/jpfss/p/7799806.html
2.Spring整合service层
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 扫描Service相关的bean-->
<context:component-scan base-package="com.Liang.service"/>
<!-- 将BookServiceImpl注入到Spring容器中-->
<bean id="BookService" class="com.Liang.service.BookServiceImpl">
<property name="bookMapper" ref="bookMapper"/>
</bean>
<!-- 配置事务管理器-->
<bean class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 注入数据库连接池-->
<property name="dataSource" ref="dataSource"/>
</bean>
</beans>
五、SpringMVC层
1.web.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<!-- 配置前端控制器-->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:application.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- SpringMVC字节码过滤-->
<filter>
<filter-name>encoding</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
2.spring-mvc.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- 扫描Controller,注册Spring容器中-->
<context:component-scan base-package="com.Liang.controller"/>
<!-- 过滤静态文件-->
<mvc:default-servlet-handler/>
<mvc:annotation-driven/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
3.Spring配置整合文件 applicationContext.xml
<?xml version="1.0" encoding="gb2312"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 导入其他配置文件-->
<import resource="spring-dao.xml"/>
<import resource="spring-service.xml"/>
<import resource="spring-mvc.xml"/>
</beans>
六、视图层和Controller
1.Controller层编写
package com.Liang.controller;
import com.Liang.pojo.Books;
import com.Liang.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 {
@Autowired
//指定具体的Bean
@Qualifier("BookService")
private BookService bookService;
//得到所有的书籍
@RequestMapping("/allBook")
public String list(Model model) {
List<Books> books = bookService.queryAllBook();
model.addAttribute("books", books);
return "allBook";
}
//添加书籍
@RequestMapping("/toAddBook")
public String addBook() {
return "addBook";
}
@RequestMapping("/addBook")
public String add(Books book) {
System.out.println(book);
int addBook = bookService.addBook(book);
if (addBook > 0) {
System.out.println("添加成功");
}
// 重定向回到列表页面
return "redirect:/book/allBook";
}
//删除书籍
@RequestMapping("/del/{bookId}")
public String delete(@PathVariable("bookId") int id) {
bookService.deleteBook(id);
return "redirect:/book/allBook";
}
//更新书籍
@RequestMapping("/toUpdateBook/{bookId}")
public String toUpdateBook(@PathVariable("bookId") int id, Model model) {
//获取更新的书籍数据通过查询指定的数据
Books book = bookService.queryById(id);
System.out.println(book);
model.addAttribute("book", book);
return "updateBook";
}
@RequestMapping("updateBook")
public String update(Model model, Books book) {
System.out.println(book);
int updateBook = bookService.updateBook(book);
if (updateBook > 0) {
System.out.println("更改成功");
}
return "redirect:/book/allBook";
}
//查找书籍
@RequestMapping("/search")
public String search(String queryBookName,Model model) //直接在设置参数得到页面的参数
{
System.out.println(queryBookName);
//判断前端参数是否为空
if(queryBookName==null||queryBookName.equals(""))
{
//得到全部书籍
return "redirect:/book/allBook";
}
//查询指定书籍查看是否为空
Books book = bookService.queryByBookName(queryBookName);
System.out.println(book);
if(book==null)
{
model.addAttribute("error","未找到");
}
List<Books> books = new ArrayList<>();
books.add(book);
//books是一个集合参数
model.addAttribute("books",books);
return "allBook";
}
}
2.进入页面: index.jsp
页面代码:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>开始界面</title>
<style type="text/css">
a{
/*颜色*/
color:darkcyan;
text-decoration:none;
/* 字体*/
font-family: cursive;
}
h2{
height:50px;
width:250px;
text-align:center;
margin: 385px auto;
background: deepskyblue;
border-radius: 4px;
line-height: 2;
}
</style>
</head>
<body>
<h2><a href="${pageContext.request.contextPath}/book/allBook">点击进入列表界面</a></h2>
</body>
</html>
页面实现:
3.书籍展示页面:allBook.jsp
页面代码:
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@page language="java" contentType="text/html;charset=UTF-8"
pageEncoding="UTF-8"%>
<html>
<head>
<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"/>
<style type="text/css">
.container{
text-align:center;
}
.btn-primary{
height:33px;
right:5px;
}
</style>
</head>
</html>
<body>
<div class="container">
<div class="row clearfix">
<div class="page-header">
<h1>
<small>书籍列表-------显示所有书籍</small>
</h1>
</div>
</div>
</div>
<%-- springboot划分网格--%>
<div class="col-md-4 column">
</div>
<div class="col-md-4 column">
</div>
<div class="col-md-4 column" style="right: 50px;left:1px">
<form action="${pageContext.request.contextPath}/book/search" method="post" class="form-horizontal" role="form">
<div class="form-group">
<span style="color:red;font-weight:bold;left: 40px;line-height: 35px;" class="span-control col-sm-2">
${error}
</span>
<div class="col-sm-8">
<input type="text" name="queryBookName" placeholder="请输入书籍名称" class="form-control"/>
</div>
<input type="submit" class="btn btn-primary col-sm-2 input-control" value="提交"/>
</div>
</form>
</div>
<%-- --%>
<div class="row clearfix">
<div class="col-md-12 column">
<table class="table table-hover table-striped">
<thred>
<tr>
<th>书籍编号</th>
<th>书籍名字</th>
<th>书籍数量</th>
<th>书籍详情</th>
<th>操作</th>
</tr>
</thred>
<tbody>
<c:forEach var="book" items="${requestScope.get('books')}">
<tr>
<td>${book.getBookId()}</td>
<td>${book.getBookName()}</td>
<td>${book.getBookCount()}</td>
<td>${book.getDetail()}</td>
<td>
<a href="${pageContext.request.contextPath}/book/toUpdateBook/${book.getBookId()}">更改</a>
<a href="${pageContext.request.contextPath}/book/del/${book.getBookId()}">删除</a>
</td>
</tr>
</c:forEach>
</tbody>
</table>
</div>
</div>
<%-- 添加--%>
<a class="btn btn-primary" href="${pageContext.request.contextPath}/book/toAddBook">
+
</a>
</body>
页面实现:
4.添加书籍页面:addBook.jsp
页面代码:
<%@ 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">
<style type="text/css">
label{
line-height: 35px;
}
.btn-primary{
margin:25px 500px;
}
.container{
text-align:center;
}
</style>
</head>
<body>
<div class="container">
<%-- row clearfix表示不受其他样式控制,就是非常独立的设置。--%>
<div class="row clearfix">
<div class="col-md-12 column">
<div class="page-header">
<h1>
<small>新增书籍</small>
</h1>
</div>
</div>
</div>
<%-- form表单,把提交的数据响应到Controller层--%>
<form action="${pageContext.request.contextPath}/book/addBook" method="post" role="form" class="from-horizontal">
<div class="form-group">
<%--@declare id="bookname"--%><label for="bookName" class="col-sm-1 label-control">书籍名称</label>
<div class="col-sm-11">
<input type="text" class="form-control" name="bookName" placeholder="请输入书籍名称"><br>
</div>
</div>
<div class="form-group">
<%--@declare id="bookcount"--%><label for="bookCount" class="col-sm-1 label-control">书籍数量</label>
<div class="col-sm-11">
<input type="text" class="form-control" name="bookCount" placeholder="请输入书籍数量"><br>
</div>
</div>
<div class="form-group">
<%--@declare id="detail"--%><label for="detail" class="col-sm-1 label-control">书籍详情</label>
<div class="col-sm-11">
<input type="text" class="form-control" name="detail"placeholder="请输入书籍详情信息"/><br>
</div>
</div>
<input type="submit" value="添加" class="btn btn-primary"/>
</form>
</div>
</body>
</html>
页面实现:
5.书籍修改页面:updateBook.jsp
页面代码:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>修改界面</title>
<meta name="viewport" content="width=device-width,initial-scale=1.0"/>
<link href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<style type="text/css">
.btn-primary{
margin:25px 500px;
}
.container{
text-align:center;
}
</style>
</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>
<%-- 请求获得book--%>
<form action="${pageContext.request.contextPath}/book/updateBook" role="form"class="form-horizontal" method="post">
<%-- 隐藏域--%>
<input type="hidden" name="bookId" value="${book.getBookId()}"/>
<div class="form-group">
<%--@declare id="bookname"--%><label for="bookName" class="col-sm-1 control-label">书籍名称</label>
<div class="col-sm-11">
<input type="text" name="bookName" class="form-control" value="${book.getBookName()}">
</div>
</div>
<div class="form-group">
<%--@declare id="bookcount"--%><label for="bookCount" class="col-sm-1 control-label" >书籍数量</label>
<div class="col-sm-11">
<input type="text" name="bookCount" class="form-control" value="${book.getBookCount()}"/>
</div>
</div>
<div class="form-group">
<%--@declare id="bookdetail"--%><label for="bookDetail" class="col-sm-1 control-label">书籍详情</label>
<div class="col-sm-11">
<input type="text" name="detail" class="form-control" value="${book.getDetail()}"/>
</div>
</div>
<input type="submit" class="btn btn-primary" value="提交">
</form>
</div>
</body>
</html>
页面实现:
6.查询功能实现: