前言:

本文内容:ssm整合SpringMVC层、ssm整合查询书籍功能、ssm整合添加书籍功能

推荐免费SpringMVC基础教程视频:【狂神说Java】SpringMVC最新教程IDEA版通俗易懂_哔哩哔哩_bilibili

ssm整合:SpringMVC层

  1. 增加web项目支持

  2. 配置web.xml

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    <?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">

    <!-- DispatcherServlet-->
    <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:spring-mvc.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>

    <!-- 配置乱码-->
    <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>
    <!-- session-->
    <session-config>
    <session-timeout>15</session-timeout>
    </session-config>
    </web-app>
  3. 新建spring-mvc.xml,进行配置

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    <?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:mvc="http://www.springframework.org/schema/mvc"
    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/mvc http://www.springframework.org/schema/cache/spring-mvc.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">

    <!-- 1.注解驱动-->
    <mvc:annotation-driven/>
    <!-- 2.静态资源过滤-->
    <mvc:default-servlet-handler/>
    <!-- 3.扫描包 controller-->
    <context:component-scan base-package="com.jokerdig.controller"/>
    <!-- 4.视图解析器-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/jsp/"/>
    <property name="suffix" value=".jsp"/>
    </bean>
    </beans>
  4. WEB-INF下新建jsp文件夹

  5. 配置完成

ssm整合:查询书籍功能

  1. 新建BookController.java,编写查询书籍

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    package com.jokerdig.controller;

    import com.jokerdig.pojo.Books;
    import com.jokerdig.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.RequestMapping;

    import java.util.List;

    /**
    * @author Joker大雄
    * @data 2022/6/14 - 15:41
    **/
    @Controller
    @RequestMapping("/book")
    public class BookController {
    // 注入service层
    @Autowired
    @Qualifier("BookServiceImpl")
    private BookService bookService;

    // 查询全部书籍
    @RequestMapping("/allBook")
    public String list(Model model){
    List<Books> books = bookService.queryAllBooks();
    model.addAttribute("list",books);
    return "allBook";
    }
    }
  2. 运行服务器,发现报错

  3. 使用Junit进行单元测试

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    package com.jokerdig.test;

    import com.jokerdig.pojo.Books;
    import com.jokerdig.service.BookService;
    import org.junit.Test;
    import org.springframework.context.support.ClassPathXmlApplicationContext;

    /**
    * @author Joker大雄
    * @data 2022/6/14 - 16:11
    **/
    public class ssmTest {
    @Test
    public void queryTest(){
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
    BookService bookServiceImpl = (BookService) context.getBean("BookServiceImpl");
    for (Books books:bookServiceImpl.queryAllBooks()){
    System.out.println(books);
    }
    }
    }
  4. 发现数据可以查询到

    1
    2
    3
    4
    5
    Books(bookID=1, bookName=Java, bookCounts=1, detail=从入门到板砖)
    Books(bookID=2, bookName=MySQL, bookCounts=10, detail=从入门到跑路)
    Books(bookID=3, bookName=Linux, bookCounts=6, detail=从入门到落网)

    Process finished with exit code 0
  5. 说明底层没有问题,错误可能是SpringMVC整合的时候没有调用到service层的Bean,最后发现在web.xml中配置文件写错了,应该改为applicationContext.xml

    web.xml

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    <?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">

    <!-- DispatcherServlet-->
    <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:applicationContext.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>

    <!-- 配置乱码-->
    <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>
    <!-- session-->
    <session-config>
    <session-timeout>15</session-timeout>
    </session-config>
    </web-app>
  6. 启动Tomcat,在进行测试

  7. 报错解决

  8. WEB-INF/jsp下新建allBook.jsp

  9. 美化index.jspallBook.jsp页面

    index.jsp

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
    <title>首页</title>
    <style>
    h3{
    width: 180px;
    height: 38px;
    margin: 100px auto;
    text-align: center;
    line-height: 38px;
    border-radius: 1em;
    background: cornflowerblue;
    }
    a{
    text-decoration: none;
    color: black;
    font-size: 20px;
    }
    </style>
    </head>
    <body>
    <h3>
    <a href="${pageContext.request.contextPath}/book/allBook">进入书籍页面</a>
    </h3>
    </body>
    </html>

    allBook.jsp

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
    <title>书籍查询</title>
    <%-- bootstrap CSS--%>
    <link href="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">

    </head>
    <body>
    <%--使用BootStrap 需要导入--%>
    <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 clearfix">
    <div class="col-md-12 column">
    <table class="table table-hover table-striped">
    <thead>
    <tr>
    <th>书籍编号</th>
    <th>书籍名称</th>
    <th>书籍数量</th>
    <th>书籍详细</th>
    </tr>
    </thead>
    <tbody>
    <%-- 从list中遍历--%>
    <c:forEach var="book" items="${list}">
    <tr>
    <td>${book.bookID}</td>
    <td>${book.bookName}</td>
    <td>${book.bookCounts}</td>
    <td>${book.detail}</td>
    </tr>
    </c:forEach>
    </tbody>
    </table>
    </div>
    </div>
    </div>
    </body>
    </html>
  10. 页面展示

    77

ssm整合:添加书籍功能

  1. 为页面编写添加书籍按钮

    allBook.jsp

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
    <title>书籍查询</title>
    <%-- bootstrap CSS--%>
    <link href="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">

    </head>
    <body>
    <%--使用BootStrap 需要导入--%>
    <div class="container">
    <%-- 栅格系统--%>
    <div class="row clearfix">
    <div class="col-md-12 column">
    <div class="page-header">
    <h1>
    <small>书籍列表</small>
    </h1>
    </div>
    </div>
    <div class="row">
    <div class="col-md-4 column">
    <a class="btn btn-primary" href="${pageContext.request.contextPath}/book/toAddBook">添加书籍</a>
    </div>
    </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>
    </tr>
    </thead>
    <tbody>
    <%-- 从list中遍历--%>
    <c:forEach var="book" items="${list}">
    <tr>
    <td>${book.bookID}</td>
    <td>${book.bookName}</td>
    <td>${book.bookCounts}</td>
    <td>${book.detail}</td>
    </tr>
    </c:forEach>
    </tbody>
    </table>
    </div>
    </div>
    </div>
    </body>
    </html>

    addBook.jsp

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
    <title>添加书籍</title>
    <%-- bootstrap CSS--%>
    <link href="https://cdn.staticfile.org/twitter-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>
    <form action="${pageContext.request.contextPath}/book/addBook" method="post">
    <div class="form-group">
    <label for="bkn">书籍名称:</label>
    <input type="text" id="bkn" class="form-control" name="bookName" required>
    </div>
    <div class="form-group">
    <label for="bkc">书籍数量:</label>
    <input type="text" id="bkc" class="form-control" name="bookCounts" required>
    </div>
    <div class="form-group">
    <label for="bkd">书籍描述:</label>
    <input type="text" id="bkd" class="form-control" name="detail" required>
    </div>
    <div class="form-group">
    <input type="submit" class="form-control" value="添加">
    </div>
    </form>
    </div>
    </div>
    </body>
    </html>
  2. 继续编写BookController.java

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    package com.jokerdig.controller;

    import com.jokerdig.pojo.Books;
    import com.jokerdig.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.RequestMapping;

    import java.util.List;

    /**
    * @author Joker大雄
    * @data 2022/6/14 - 15:41
    **/
    @Controller
    @RequestMapping("/book")
    public class BookController {
    // 注入service层
    @Autowired
    @Qualifier("BookServiceImpl")
    private BookService bookService;

    // 查询全部书籍
    @RequestMapping("/allBook")
    public String list(Model model){
    List<Books> books = bookService.queryAllBooks();
    model.addAttribute("list",books);
    return "allBook";
    }
    // 跳转到添加书籍
    @RequestMapping("/toAddBook")
    public String toAddBook(){
    return "addBook";
    }
    // 添加书籍
    @RequestMapping("/addBook")
    public String addBook(Books books){
    bookService.addBook(books);
    return "redirect:/book/allBook";
    }
    }
  3. 添加数据测试

    添加成功

    76