IDEA+Maven搭建SSM框架项目
创建数据库
本文数据库使用MySQL 5.6.24,使用SQLyog来进行操作。
首先创建一个名为library的数据库,在数据库中创建一张名为book的表,表结构如下图所示(注意引擎、字符集、核对)。
创建工程
- 打开maven的安装路径,找到conf文件夹下的setting.xml文件,打开。
在mirrors标签里添加如下代码。
<mirror>
<id>alimaven</id>
<mirrorOf>central</mirrorOf>
<name>aliyun maven</name>
<url>http://maven.aliyun.com/nexus/content/repositories/central/</url>
</mirror>
具体如下图所示。
- 点击File->New->Project,选择maven,选择自己安装的JDK,将Create from archetype打勾,选择org.apache.maven.archetypesmaven-archetype-webapp,注意不要选成上面的cocoon-22-archetype-webapp,选择之后点击next。
- 输入公司名,项目名,版本。
4. 选择maven的路径,可以选择IDEA自带的maven,也可以选择自己安装的maven;User settings File选择第一步中maven的安装路径下的setting.xml文件。如果C盘的用户名下有.m2文件夹,可以将setting.xml文件复制到.m2文件夹下,在此处选择.m2文件夹下的setting.xml文件。
5. 出现了BUILD SUCCESS即完成了工程的创建。此时右下角可能会出现一个小提示框:Maven projects need to be imported,选择Enable Auto-import。
写项目
- 在工程的src/main目录下面新建两个目录java和resources:右击main文件夹->New->Directory,输入目录名。
建好之后如下图。
- 右击java目录,选择Mark Directory as,选择Sources Root;右击resources目录,选择Mark Directory as,选择Resources Root,如下图所示。
- 在工程的根目录下面新建target文件夹,系统自动将其设置为“Excluded” 。
- 在刚才新建的java文件下新建“com”包,再在com包下新建四个包,分别命名为:pojo,dao,service,controller。其中pojo包存放自定义的java类。如:paper类,user类,book类等,每个类的属性设为private,并提供public属性的getter/setter方法让外界访问;dao包定义接口,包含与数据库进行交互的功能;service包定义接口,包含系统所提供的功能。(之后还会在service包下再新建impl包);controller:控制器,负责接收页面请求,转发和处理。
- 在resources目录下新建Directory:“mapper”(用于存放xxxMapper.xml文件)和“spring”(用于存放spring-xxx.xml配置文件),新建文件:“jdbc.properties”(mysql数据库配置文件),”log4j.properties”(日志输出配置文件),”mybatis-config.xml”(mybatis框架配置文件)。 建好之后整个工程的目录如下图所示。
- 在工程中的pom.xml文件配置项目所需要的jar包。
首先修改properties标签:
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<!-- spring版本号 -->
<spring.version>5.0.2.RELEASE</spring.version>
<!-- mybatis版本号 -->
<mybatis.version>3.2.6</mybatis.version>
<!-- log4j日志文件管理包版本 -->
<slf4j.version>1.7.7</slf4j.version>
<log4j.version>1.2.17</log4j.version>
<c3p0.version>0.9.5.2</c3p0.version>
<taglibs.version>1.1.2</taglibs.version>
</properties>
接着修改dependencies标签:
<dependencies>
<!-- spring核心包 -->
<!-- 单元测试 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- 第一部分:Spring 配置-->
<!-- Spring core -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- Spring DAO -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- Spring mvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- 第二部分:Servlet web -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.2</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.4</version>
</dependency>
<!-- 第三部分:数据库和mybatis -->
<!-- 数据库 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.38</version>
</dependency>
<!-- 数据库连接池 -->
<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>${mybatis.version}</version>
</dependency>
<!-- mybatis-spring整合包 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.1</version>
</dependency>
<!-- 第四部分:日志 -->
<!-- 实现slf4j接口并整合 -->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.1.1</version>
</dependency>
</dependencies>
- 自定义类。根据本文例子所用数据库的表,定义Book类。右击pojo包->New->Java Class,新建Book类,类里有bookId,bookName,bookAuthor,bookContent,bookAddDate五个属性,并添加Getter和Setter方法。注意不能有构造方法,可以重写toString方法。
package com.pojo;
public class Book {
private String bookId;
private String bookName;
private String bookAuthor;
private String bookContent;
private String bookAddDate;
public String getBookId() {
return bookId;
}
public void setBookId(String bookId) {
this.bookId = bookId;
}
public String getBookName() {
return bookName;
}
public void setBookName(String bookName) {
this.bookName = bookName;
}
public String getBookAuthor() {
return bookAuthor;
}
public void setBookAuthor(String bookAuthor) {
this.bookAuthor = bookAuthor;
}
public String getBookContent() {
return bookContent;
}
public void setBookContent(String bookContent) {
this.bookContent = bookContent;
}
public String getBookAddDate() {
return bookAddDate;
}
public void setBookAddDate(String bookAddDate) {
this.bookAddDate = bookAddDate;
}
}
- 在dao包中新建接口BookDao,接口里面有对Book表的增删改查方法。其具体实现在后面的BookMapper.xml中。
package com.dao;
import com.pojo.Book;
import java.util.List;
public interface BookDao {
int addBook(Book book);
int deleteBookById(String id);
int updateBook(Book book);
Book queryById(String id);
List<Book> queryAllBook();
}
- 在service包中新建接口BookService,接口里面包含所有的业务。
package com.service;
import com.pojo.Book;
import java.util.List;
public interface BookService {
int addBook(Book book);
int deleteBookById(String id);
int updateBook(Book book);
Book queryById(String id);
List<Book> queryAllBook();
}
- 右击service包,新建impl包,在impl包中新建BookServiceImpl类,继承BookService接口,用来具体实现BookService接口中的方法。
注意:
a. 在接口上方添加注解@Service,此注解用来将service放入spring容器中。
b. 在类属性上方添加注解@Autowired,此注解用来让spring自动创建对象。
package com.service.impl;
import com.dao.BookDao;
import com.pojo.Book;
import com.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class BookServiceImpl implements BookService {
@Autowired
private BookDao bookDao;
@Override
public int addBook(Book book) {
return bookDao.addBook(book);
}
@Override
public int deleteBookById(String id) {
return bookDao.deleteBookById(id);
}
@Override
public int updateBook(Book book) {
return bookDao.updateBook(book);
}
@Override
public Book queryById(String id) {
return bookDao.queryById(id);
}
@Override
public List<Book> queryAllBook() {
return bookDao.queryAllBook();
}
}
- 在controller包中新建类BookController,用来控制页面的跳转。
注意:
a. 在类上方添加注解@Controller和注解@RequestMapping("/book"),引号内容根据程序而定。
b. 在属性上方添加注解@Autowired,用来自动创建对象。
c. 在每个方法上方添加注解@RequestMapping("/******"),引号内容根据程序而定。
package com.controller;
import com.pojo.Book;
import com.service.BookService;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/book")
public class BookController {
@Autowired
private BookService bookService;
@RequestMapping("/allBook")
public String list(Model model) {
List<Book> list = bookService.queryAllBook();
model.addAttribute("list", list);
return "allBook";
}
@RequestMapping("toAddBook")
public String toAddBook() {
return "addBook";
}
@RequestMapping("/addBook")
public String addBook(Book book) {
bookService.addBook(book);
return "redirect:/book/allBook";
}
@RequestMapping("/del/{bookId}")
public String deleteBook(@PathVariable("bookId") String id) {
bookService.deleteBookById(id);
return "redirect:/book/allBook";
}
@RequestMapping("toUpdateBook")
public String toUpdateBook(Model model, String id) {
model.addAttribute("book", bookService.queryById(id));
return "updateBook";
}
@RequestMapping("/updateBook")
public String updateBook(Model model, Book book) {
bookService.updateBook(book);
book = bookService.queryById(book.getBookId());
model.addAttribute("book", book);
return "redirect:/book/allBook";
}
}
- 新建mybatis框架mapper代理开发配置文件:右击resources目录中的mapper->New->File,输入BookMapper.xml。此文件用来具体实现BookDao中的增删改查方法。
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.dao.BookDao">
<resultMap type="Book" id="bookResultMap" >
<id property="bookId" column="id"/>
<result property="bookName" column="name"/>
<result property="bookAuthor" column="author"/>
<result property="bookContent" column="content"/>
<result property="bookAddDate" column="add_date"/>
</resultMap>
<insert id="addBook" parameterType="Book">
INSERT INTO book(id,name,author,content,add_date) VALUE (#{bookId},#{bookName}, #{bookAuthor}, #{bookContent}, #{bookAddDate})
</insert>
<delete id="deleteBookById" parameterType="String">
DELETE FROM book WHERE id=#{bookId}
</delete>
<update id="updateBook" parameterType="Book">
UPDATE book
SET name = #{bookName}, author = #{bookAuthor}, content = #{bookContent}
WHERE id = #{bookId}
</update>
<select id="queryById" resultType="Book" parameterType="String">
SELECT id,name,author,content,add_date
FROM book
WHERE id=#{bookId}
</select>
<select id="queryAllBook" resultMap="bookResultMap">
SELECT id,name,author,content,add_date
FROM book
</select>
</mapper>
- 在resource/spring包下分别新建配置文件:spring-dao.xml , spring-mvc.xml , spring-service.xml。新建时右击spring包,选择New->XML Configuration File->Spring Config,如下图所示。
spring-dao.xml(spring-mybatis整合配置文件)文件内容:
<?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">
<!-- 配置整合mybatis过程 -->
<!-- 1.配置数据库相关参数properties的属性:${url} -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- 2.数据库连接池 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<!-- 配置连接池属性 -->
<property name="driverClass" value="${jdbc.driver}"/>
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="user" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<!-- c3p0连接池的私有属性 -->
<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"/>
<!-- 配置MyBaties全局配置文件:mybatis-config.xml -->
<property name="configLocation" value="classpath:mybatis-config.xml"/>
<!-- 扫描pojo包 使用别名 -->
<property name="typeAliasesPackage" value="com.pojo"/>
<!-- 扫描sql配置文件:mapper需要的xml文件 -->
<property name="mapperLocations" value="classpath:mapper/*.xml"/>
</bean>
<!-- 4.配置扫描Dao接口包,动态实现Dao接口,注入到spring容器中 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 注入sqlSessionFactory -->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
<!-- 给出需要扫描Dao接口包 -->
<property name="basePackage" value="com.dao"/>
</bean>
</beans>
spring-mvc.xml(spring mvc配置文件)文件内容:
<?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-3.0.xsd">
<!-- 配置SpringMVC -->
<!-- 1.开启SpringMVC注解模式 -->
<!-- 简化配置:
(1)自动注册DefaultAnootationHandlerMapping,AnotationMethodHandlerAdapter
(2)提供一些列:数据绑定,数字和日期的format @NumberFormat, @DateTimeFormat, xml,json默认读写支持
-->
<mvc:annotation-driven />
<!-- 2.静态资源默认servlet配置
(1)加入对静态资源的处理:js,gif,png
(2)允许使用"/"做整体映射
-->
<mvc:default-servlet-handler/>
<!-- 3.配置jsp 显示ViewResolver -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
<!-- 4.扫描web相关的bean -->
<context:component-scan base-package="com.controller" />
</beans>
spring-service.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:tx="http://www.springframework.org/schema/tx"
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/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 扫描service包下所有使用注解的类型 -->
<context:component-scan base-package="com.service" />
<!-- 配置事务管理器 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 注入数据库连接池 -->
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 配置基于注解的声明式事务 -->
<tx:annotation-driven transaction-manager="transactionManager" />
</beans>
- 在在jdbc.properties文件中配置mysql数据库,注意这里的数据库名、表名,用户名,密码要根据自身情况来修改。
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/library?useUnicode=true&characterEncoding=utf8
jdbc.username=root
jdbc.password=root
- 在log4j.properties文件中配置日志设定。
log4j.rootLogger=ERROR, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
- 在mybatis-config.xml文件中配置mybtis框架。
<?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>
<!-- 配置全局属性 -->
<settings>
<!-- 使用jdbc的getGeneratedKeys获取数据库自增主键值 -->
<setting name="useGeneratedKeys" value="true" />
<!-- 使用列别名替换列名 默认:true -->
<setting name="useColumnLabel" value="true" />
<!-- 开启驼峰命名转换:Table{create_time} -> Entity{createTime} -->
<setting name="mapUnderscoreToCamelCase" value="true" />
</settings>
</configuration>
- 配置web.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<!-- Servlet Filters ================================================ -->
<!--
- Declare a filter for multipart MIME handling
-->
<display-name>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 配置springMVC需要加载的配置文件
spring-dao.xml,spring-service.xml,spring-mvc.xml
Mybatis - > spring -> springmvc
-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/spring-*.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<!-- 默认匹配所有的请求 -->
<url-pattern>/</url-pattern>
</servlet-mapping>
<!--
- Declare a filter for debugging incoming request
-->
<filter>
<filter-name>encodingFilter</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>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
- 在刚才新建的jsp目录中新建三个jsp文件:addBook.jsp,allBook.jsp,updateBook.jsp。这是用来向用户展示的界面。
addBook.jsp内容:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<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>
图书管理系统
</h1>
</div>
</div>
</div>
<div class="row clearfix">
<div class="col-md-12 column">
<div class="page-header">
<h1>
<small>新增图书</small>
</h1>
</div>
</div>
</div>
<form action="" name="userForm">
编号:<input type="text" name="bookId"><br><br><br>
书名:<input type="text" name="bookName"><br><br><br>
作者:<input type="text" name="bookAuthor"><br><br><br>
简介:<input type="text" name="bookContent"><br><br><br>
添加时间:<input type="text" name="bookAddDate"><br><br><br>
<input type="button" value="添加" onclick="addBook()">
</form>
<script type="text/javascript">
function addBook() {
var form = document.forms[0];
form.action = "<%=basePath %>book/addBook";
form.method = "post";
form.submit();
}
</script>
</div>
allBook.jsp内容:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<% String appPath = request.getContextPath(); %>
<html>
<head>
<title>Book列表</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>
图书管理系统
</h1>
</div>
</div>
</div>
<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="${path}/book/toAddBook">新增</a>
</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>
<th>操作</th>
</tr>
</thead>
<tbody>
<c:forEach var="book" items="${requestScope.get('list')}" varStatus="status">
<tr>
<td>${book.bookId}</td>
<td>${book.bookName}</td>
<td>${book.bookAuthor}</td>
<td>${book.bookContent}</td>
<td>${book.bookAddDate}</td>
<td>
<a href="${path}/book/toUpdateBook?id=${book.bookId}">更改</a> |
<a href="<%=appPath%>/book/del/${book.bookId}">删除</a>
</td>
</tr>
</c:forEach>
</tbody>
</table>
</div>
</div>
</div>
updateBook.jsp内容:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<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>
图书管理系统
</h1>
</div>
</div>
</div>
<div class="row clearfix">
<div class="col-md-12 column">
<div class="page-header">
<h1>
<small>修改图书信息</small>
</h1>
</div>
</div>
</div>
<form action="" name="userForm">
<input type="hidden" name="bookId" value="${book.bookId}"/>
书名:<input type="text" name="bookName" value="${book.bookName}"/>
作者:<input type="text" name="bookAuthor" value="${book.bookAuthor}"/>
简介:<input type="text" name="bookContent" value="${book.bookContent }"/>
<input type="button" value="提交" onclick="updateBook()"/>
</form>
<script type="text/javascript">
function updateBook() {
var form = document.forms[0];
form.action = "<%=basePath %>book/updateBook";
form.method = "post";
form.submit();
}
</script>
</div>
- 修改index.jsp文件。
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" %>
<%
pageContext.setAttribute("path", request.getContextPath());
%>
<!DOCTYPE HTML>
<html>
<head>
<title>首页</title>
<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>
<div class="container">
<div class="row clearfix">
<div class="col-md-12 column">
<div class="page-header">
<h1>
图书管理系统
</h1>
</div>
</div>
</div>
</div>
<br><br>
<h3>
<a href="${path }/book/allBook">点击进入管理页面</a>
</h3>
</body>
</html>
利用Tomcat进行部署
- 点击上方的Run->Edit Configurations。
- 点击左上角“+”,选择Tomcat Server->Local,进入界面后更改Name(也可以不改),选择安装在本地的Tomcat地址。接着点击Deployment,点击“+”,选择Artifact,选择war exploded。之后出现的Application context选择空的那个。选择完之后点击Apply。
3. 点击Run运行,查看效果。
参考博客
https://blog.csdn.net/weixin_42747657/article/details/81160501