Maven整合SSM测试

前面也说到了关于SSM的整合,话不多说直接从创建项目开始CRUD之路(参考前面写过的Mybatis和Spring整合SSM简单整合),这是整个项目的结构

Maven整合SSM测试

  以及最终的结果。(附上下载地址

Maven整合SSM测试

一、创建Maven项目

1、在Project中按照下面步骤进行创建一个新的maven项目

Maven整合SSM测试

2、然后填写项目的坐标信息

Maven整合SSM测试

3、最后选择Maven项目本地仓库的路径,然后下一步填写项目名即可

Maven整合SSM测试

二、配置pom.xml文件,导入相关的jar包

  1、以配置spring-webmvc为例:我们在https://mvnrepository.com/按照下面的方式都说想要导入的jar包

  Maven整合SSM测试

  2、然后选择合适的版本之后,赋值Maven配置文件

  Maven整合SSM测试

  3、在pom.xml中配置如下,然后在左侧Maven可以发现工程依赖的jar包,如果pom.xml中配置时发现没有提示没有本地库中没有对应的jar包,如下所示  

  Maven整合SSM测试

  我们不用理会,Maven会自动下载配置的所需要的jar包,如下所示

  Maven整合SSM测试

  4、上述导入的包有spring、springmvc以及mybatis和其他如数据库连接池、驱动和测试包等都导入完毕之后,整个pom。xml就是这样

 <?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion>
<packaging>war</packaging> <name>ssm-first</name>
<groupId>cn.test.ssm</groupId>
<artifactId>ssm-first</artifactId>
<version>1.0-SNAPSHOT</version> <dependencies>
<dependency>
<groupId>javaee</groupId>
<artifactId>javaee-api</artifactId>
<version>5</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.7.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.3.7.RELEASE</version>
</dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.7.RELEASE</version>
</dependency> <!--Spring的测试包-->
<!-- https://mvnrepository.com/artifact/org.springframework/spring-test -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.3.7.RELEASE</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/aopalliance/aopalliance -->
<dependency>
<groupId>aopalliance</groupId>
<artifactId>aopalliance</artifactId>
<version>1.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/asm/asm -->
<dependency>
<groupId>asm</groupId>
<artifactId>asm</artifactId>
<version>3.3.1</version>
</dependency> <!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.10</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.javassist/javassist -->
<dependency>
<groupId>org.javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.17.1-GA</version>
</dependency> <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.mchange/c3p0 -->
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1.2</version>
</dependency>
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.2</version>
</dependency>
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>2.2.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.41</version>
</dependency>
<!-- https://mvnrepository.com/artifact/jstl/jstl -->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet/servlet-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet.jsp/jsp-api -->
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.1</version>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mybatis.generator/mybatis-generator-core -->
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.5</version>
</dependency> </dependencies> </project>

三、配置web.xml

  其中包括Spring容器的配置(Tomcat启动时候加载Spring的有关配置文件)、前端控制器DispatcherServlet的配置和前端控制器拦截请求的配置、编码过滤器的配置、以及实现REST风格的过滤器配置。

 <?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"> <!--配置启动时候加载Spring相关配置文件-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:spring/applicationContext-*.xml</param-value>
</context-param>
<!--配置监听器-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--配置前端控制器-->
<!--配置前端控制器-->
<servlet>
<servlet-name>SpringMvc</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<!--
配饰SpringMVC的配置文件(处理器映射器、适配器等)
注明需要这样配置的原因:自己配置contextConfigLocation,就不会自己默认加载/WEB-INF/下面的dispatch-servlet.xml
-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc/springmvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>SpringMvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!--编码过滤器-->
<filter>
<filter-name>CharacterEncodingFilter</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>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!--使用REST风格的URI-->
<filter>
<!--将页面上的post请求转为指定的delete或者put请求-->
<filter-name>HiddenHttpMethodFilter</filter-name>
<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>HiddenHttpMethodFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>

四、配置SpringMVC的配置文件

  springmvc.xml中的配置主要包括Controller层的扫描配置、处理器映射器、处理器适配器、视图解析器的配置,和一些静态资源请求交由tomcat处理的配置

 <?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"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd "> <!--配置controller的扫描-->
<context:component-scan base-package="controller"></context:component-scan> <!--将springmvc不能够处理的请求交给Tomcat-->
<mvc:default-servlet-handler></mvc:default-servlet-handler> <!--配置mvc:annotation代替基于注解方式的处理器映射器和适配器的配置-->
<mvc:annotation-driven></mvc:annotation-driven> <!--配置视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>

五、配置Spring的配置文件

  其中主要包括的就是数据源、会话工厂、mapper扫描器和事务控制

 <?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:aop="http://www.springframework.org/schema/aop"
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/aop
http://www.springframework.org/schema/aop/spring-aop.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"> <!--加载数据库信息的配置文件-->
<context:property-placeholder location="classpath:db.properties"></context:property-placeholder> <!--配置数据源-->
<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}" />
</bean> <!--配置SqlSessionFactory-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--加载Mybatis的配置文件-->
<property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml"></property>
<!--配置数据源-->
<property name="dataSource" ref="dataSource"></property>
<!--指定mapper配置文件所在的位置-->
<property name="mapperLocations" value="classpath:mapper/*.xml"></property>
</bean> <!--配置mapper扫描器-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="dao"></property>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean> <!--
事务控制的配置
对数据库操作Mybatis的事务控制使用spring的jdbc事务管理控制类
-->
<!--事务管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!--添加对数据源的控制-->
<property name="dataSource" ref="dataSource"></property>
</bean> <!--通知-->
<tx:advice id="txAdvice">
<tx:attributes>
<!--配置传播行为-->
<!--配置必须进行事务控制的方法-->
<tx:method name="*"></tx:method>
<tx:method name="save*" propagation="REQUIRED"/>
<tx:method name="delete*" propagation="REQUIRED"></tx:method>
<tx:method name="insert*" propagation="REQUIRED"></tx:method>
<tx:method name="update*" propagation="REQUIRED"></tx:method>
<!--配置支持事务的方法-->
<tx:method name="find*" propagation="SUPPORTS" read-only="true"></tx:method>
</tx:attributes>
</tx:advice>
<!--配置aop去调用通知-->
<aop:config>
<aop:advisor advice-ref="txAdvice" pointcut="execution(* service.*.*(..))"></aop:advisor>
</aop:config> </beans>

六、Mybatis逆向工程生成相关代码

  1、首先在数据库中创建我们所需要的两张表user(员工)、department(部门),并且建立两者之间的外键关联关系(sql文件下载

 CREATE TABLE `t_user` (
`user_id` INT(11) NOT NULL AUTO_INCREMENT,
`user_name` VARCHAR(20) DEFAULT NULL,
`user_sex` VARCHAR(20) DEFAULT NULL,
`user_email` VARCHAR(40) DEFAULT NULL,
`dep_id` INT(11) DEFAULT NULL,
PRIMARY KEY (`user_id`),
KEY `FK_t_user` (`dep_id`),
CONSTRAINT `FK_t_user` FOREIGN KEY (`dep_id`) REFERENCES `t_department` (`department_id`)
) ENGINE=INNODB DEFAULT CHARSET=utf8
 CREATE TABLE `t_department` (
`department_id` INT(11) NOT NULL AUTO_INCREMENT,
`department_name` VARCHAR(255) NOT NULL,
PRIMARY KEY (`department_id`)
) ENGINE=INNODB DEFAULT CHARSET=utf8

  2、使用Mybatis逆向工程

  ①打开http://www.mybatis.org/generator/,进入Quick Start Guide

  Maven整合SSM测试

  ②然后根据里面的代码实例进行逆向工程配置文件的配置

  Maven整合SSM测试

  ③配置mybatis-generator.xml

 <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd"> <generatorConfiguration> <context id="DB2Tables" targetRuntime="MyBatis3">
<!--配置数据库信息-->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql:///ssm-crud"
userId="root"
password="123">
</jdbcConnection> <javaTypeResolver>
<property name="forceBigDecimals" value="false" />
</javaTypeResolver> <!--逆向工程生成的bean所在的位置-->
<javaModelGenerator targetPackage="cn.test.ssm.bean" targetProject=".\src\main\java">
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
</javaModelGenerator> <!--指定sql映射文件的位置-->
<sqlMapGenerator targetPackage="mapper" targetProject=".\src\main\resources">
<property name="enableSubPackages" value="true" />
</sqlMapGenerator> <!--指定mapper接口的位置-->
<javaClientGenerator type="XMLMAPPER" targetPackage="cn.test.ssm.dao" targetProject=".\src\main\java">
<property name="enableSubPackages" value="true" />
</javaClientGenerator> <!--指定每个表的生成策略-->
<table tableName="t_user" domainObjectName="Employee" ></table>
<table tableName="t_department" domainObjectName="Department"></table> </context>
</generatorConfiguration>

  ④在刚刚的网站首页中进入下图所示的链接然后根据里面的程序改写运行逆向工程

  Maven整合SSM测试

  Maven整合SSM测试

  ⑤根据上面的示例程序编写逆向工程测试程序

 package cn.test.ssm.utils;

 import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.internal.DefaultShellCallback; import java.io.File;
import java.util.ArrayList;
import java.util.List; public class Generator { public static void main(String[] args) throws Exception {
List<String> warnings = new ArrayList<String>();
boolean overwrite = true;
File configFile = new File("mybatis-generator.xml");
ConfigurationParser cp = new ConfigurationParser(warnings);
Configuration config = cp.parseConfiguration(configFile);
DefaultShellCallback callback = new DefaultShellCallback(overwrite);
MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
myBatisGenerator.generate(null);
}
}

  ⑥然后运行程序可以发现根据Maven整合SSM测试中的配置文件生成了代码

  Maven整合SSM测试

七、Junit测试

  在整合前后端时,我们先使用测试程序测试逆向工程以及自己添加的其他方法的正确性。这里我们使用Spring提供成测试模块进行测试,首先在pom.xml中引入spring-test的jar包

    <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.3.7.RELEASE</version>
<scope>test</scope>
</dependency>

  2、然后建立测试包,其中使用ContextConfiguration指定spring的配置文件所在位置,以及Runwith指定使用spring的测试模块进行测试,然后就可以使用AutoWried注入我们想要测试的mapper接口了。下面测试的是DepartmentMapper中的insert方法(其余的测试方法可以下载源代码,已放置在github上面,可以参考)

 package cn.test.ssm;

 import cn.test.ssm.bean.Department;
import cn.test.ssm.bean.Employee;
import cn.test.ssm.dao.DepartmentMapper;
import cn.test.ssm.dao.EmployeeMapper;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; /*指定使用spring提供的单元测试模块进行*/
@RunWith(SpringJUnit4ClassRunner.class)
/*指定使用spring的单元测试进行,配置spring的配置文件路径即可*/
@ContextConfiguration(locations = {"classpath*:spring/applicationContext.xml"})
public class DaoTest { @Autowired
private DepartmentMapper departmentMapper; @Autowired
private EmployeeMapper employeeMapper; @Test
public void testInsertDept() throws Exception { /*开始进行测试*/
//1、insert部门信息
Department d = new Department();
d.setDepartmentName("管理部");
departmentMapper.insertSelective(d);
} @Test
public void testInsertUser() throws Exception { Employee e = new Employee();
e.setUserName("Lucy");
e.setUserSex("男");
e.setUserEmail("test@qq.com");
e.setDepId(1);
employeeMapper.insertSelective(e);
}
}

测试结果:

Maven整合SSM测试

Maven整合SSM测试

八、实现相应的功能

  1、访问index页面时,显示所有员工列表信息

  a)为了支持后台的分页查询,我们可以使用pagehelper,首先在pom.xml中引入依赖的jar包

    <!--Mybatis的分页插件依赖包-->
<!-- https://mvnrepository.com/artifact/com.github.pagehelper/pagehelper -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.0.0</version>
</dependency>  

  然后这是引入上述依赖包之后多出了下面这两项jar包内容

  Maven整合SSM测试

  b)然后就是在Mybatis的核心配置文件SqlMapConfig.xml中导入插件

    <plugins>
<plugin interceptor="com.github.pagehelper.PageInterceptor">
</plugin>
</plugins>

  c)然后在Controller层的方法中使用这个插件

     @RequestMapping("/emps")
public String getEmps(@RequestParam(value = "pageNum", defaultValue = "1")Integer pageNum, Model model) throws Exception{
//使用分页查询
//使用pageHelper插件,在查询之前只需要调用(传入页码和条目数)
PageHelper.startPage(pageNum, 8);
//startPage后面紧跟的查询就是分页查询
List<Employee> employeeList = employeeService.getAll(); //使用PageInfo,PageInfo包含了非常全面的分页属性
//将查询的结果信息放在pageInfo中,并且指定连续显示8页
PageInfo pageInfo = new PageInfo(employeeList, 8); model.addAttribute("pageInfo", pageInfo);
return "list";
}

  2、实现列表分页查询

  a)我们首先要做的就是访问的时候显示首页信息,我们需要编写对应的方法,

  下面的方法中,接受前台传入的页码参数,然后返回指定条数的信息到页面中Maven整合SSM测试

 package cn.test.ssm.controller;

 import cn.test.ssm.bean.Employee;
import cn.test.ssm.service.EmployeeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView; import java.util.List; @Controller
public class EmployeeController { @Autowired
EmployeeService employeeService; @RequestMapping("/emps.action")
public ModelAndView getEmps(@RequestParam(value = "pageNum", defaultValue = "1")Integer pageNum) throws Exception{
//使用分页查询
//使用pageHelper插件,在查询之前只需要调用(传入页码和条目数)
PageHelper.startPage(pageNum, 8);
//startPage后面紧跟的查询就是分页查询
List<Employee> employeeList = employeeService.getAll(); //使用PageInfo,PageInfo包含了非常全面的分页属性
//将查询的结果信息放在pageInfo中,并且指定连续显示8页
PageInfo pageInfo = new PageInfo(employeeList, 5); ModelAndView modelAndView = new ModelAndView(); modelAndView.addObject("pageInfo", pageInfo);
modelAndView.setViewName("WEB-INF/views/list.jsp");
//return "/WEB-INF/views/list.jsp";
return modelAndView;
}
}

  b)然后写service的方法,dao层我们使用的是逆向工程直接生成对应的mapper映射文件和mapper接口,所以在service中注入mapper接口后,直接调用mapper接口中的方法

     /*
查询员工列表信息
*/
public List<Employee> getAll() {
return employeeMapper.selectByExampleAndDepartment(null);
}

  c)编写好相应的前台jsp代码,进行测试(使用bootstrap简单布局一下)

 <%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>员工列表</title>
<script type="text/javascript"
src="${pageContext.request.contextPath }/static/js/jquery-1.12.4.min.js"></script>
<link
href="${pageContext.request.contextPath }/static/bootstrap-3.3.7-dist/css/bootstrap.min.css"
rel="stylesheet">
<script
src="${pageContext.request.contextPath }/static/bootstrap-3.3.7-dist/js/bootstrap.min.js"></script>
</head>
<body>
<!-- 搭建显示页面 -->
<div class="container">
<!-- 标题 -->
<div class="row">
<div class="col-md-12">
<h1>SSM整合测试</h1>
</div>
</div>
<!-- 按钮 -->
<div class="row">
<div class="col-md-4 col-md-offset-8">
<button class="btn btn-primary">新增</button>
<button class="btn btn-danger">删除</button>
</div>
</div>
<!-- 显示表格数据 -->
<div class="row">
<div class="col-md-12">
<table class="table table-hover">
<tr>
<th>编号</th>
<th>名字</th>
<th>性别</th>
<th>邮箱</th>
<th>部门</th>
<th>操作</th>
</tr>
<c:forEach items="${pageInfo.list }" var="emp">
<tr>
<th>${emp.userId }</th>
<th>${emp.userName }</th>
<th>${emp.userSex}</th>
<th>${emp.userEmail }</th>
<th>${emp.department.departmentName }</th>
<th>
<button class="btn btn-primary btn-sm">
<span class="glyphicon glyphicon-pencil" aria-hidden="true"></span>
编辑
</button>
<button class="btn btn-danger btn-sm">
<span class="glyphicon glyphicon-trash" aria-hidden="true"></span>
删除
</button>
</th>
</tr>
</c:forEach>
</table>
</div>
</div> <!-- 显示分页信息 -->
<div class="row">
<!--分页文字信息 -->
<div class="col-md-6">当前 ${pageInfo.pageNum }页,总${pageInfo.pages }
页,总 ${pageInfo.total } 条记录</div>
<!-- 分页条信息 -->
<div class="col-md-6">
<nav aria-label="Page navigation">
<ul class="pagination">
<li><a href="${pageContext.request.contextPath }/emps?pn=1">首页</a></li>
<c:if test="${pageInfo.hasPreviousPage }">
<li><a href="${pageContext.request.contextPath }/emps?pn=${pageInfo.pageNum-1}"
aria-label="Previous"> <span aria-hidden="true">&laquo;</span>
</a></li>
</c:if> <c:forEach items="${pageInfo.navigatepageNums }" var="page_Num">
<c:if test="${page_Num == pageInfo.pageNum }">
<li class="active"><a href="#">${page_Num }</a></li>
</c:if>
<c:if test="${page_Num != pageInfo.pageNum }">
<li><a href="${pageContext.request.contextPath }/emps?pn=${page_Num }">${page_Num }</a></li>
</c:if> </c:forEach>
<c:if test="${pageInfo.hasNextPage }">
<li><a href="${pageContext.request.contextPath }/emps?pn=${pageInfo.pageNum+1 }"
aria-label="Next"> <span aria-hidden="true">&raquo;</span>
</a></li>
</c:if>
<li><a href="${pageContext.request.contextPath }/emps?pn=${pageInfo.pages}">末页</a></li>
</ul>
</nav>
</div>
</div> </div>
</body>
</html>

前台员工信息列表

  d)下面先测试一下,能否访问首页,地址栏中输入http://localhost:8080/ssm_crud/index.jsp

  index.jsp中转发到了emps.action(处理器映射器会将该url映射到我们刚刚编写的Controller层的getEmps()方法,然后返回首页的数据)

 Maven整合SSM测试

上一篇:Rabbit hunt


下一篇:微信APP支付服务端开发Java版(一)