目录
Spring提供了监听器ContextLoaderListener,实现ServletContextListener接口
可监听 ServletContext的状态,在web服务器的启动,读取Spring的配置文件,创建Spring的IOC容器。
web 应用中必须在web.xml中配置
- <listener>
- <listenerclass>org.springframework.web.context.ContextLoaderListenerlistener-class>
- listener>
- <context-param>
- <param-name>contextConfigLocationparam-name>
- <param-value>classpath:spring.xmlparam-value>
- context-param>
- "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/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0modelVersion>
-
- <groupId>com.atguigu.ssmgroupId>
- <artifactId>ssmartifactId>
- <version>1.0-SNAPSHOTversion>
-
- <packaging>warpackaging>
- <properties>
-
- <spring.version>5.3.1spring.version>
- properties>
- <dependencies>
- <dependency>
- <groupId>org.springframeworkgroupId>
- <artifactId>spring-contextartifactId>
- <version>${spring.version}version>
- dependency>
- <dependency>
- <groupId>org.springframeworkgroupId>
- <artifactId>spring-beansartifactId>
- <version>${spring.version}version>
- dependency>
-
- <dependency>
- <groupId>org.springframeworkgroupId>
- <artifactId>spring-webartifactId>
- <version>${spring.version}version>
- dependency>
- <dependency>
- <groupId>org.springframeworkgroupId>
- <artifactId>spring-webmvcartifactId>
- <version>${spring.version}version>
- dependency>
- <dependency>
- <groupId>org.springframeworkgroupId>
- <artifactId>spring-jdbcartifactId>
- <version>${spring.version}version>
- dependency>
- <dependency>
- <groupId>org.springframeworkgroupId>
- <artifactId>spring-aspectsartifactId>
- <version>${spring.version}version>
- dependency>
- <dependency>
- <groupId>org.springframeworkgroupId>
- <artifactId>spring-testartifactId>
- <version>${spring.version}version>
- dependency>
-
- <dependency>
- <groupId>org.mybatisgroupId>
- <artifactId>mybatisartifactId>
- <version>3.5.7version>
- dependency>
-
- <dependency>
- <groupId>org.mybatisgroupId>
- <artifactId>mybatis-springartifactId>
- <version>2.0.6version>
- dependency>
-
- <dependency>
- <groupId>com.alibabagroupId>
- <artifactId>druidartifactId>
- <version>1.0.9version>
- dependency>
-
- <dependency>
- <groupId>junitgroupId>
- <artifactId>junitartifactId>
- <version>4.12version>
- <scope>testscope>
- dependency>
-
- <dependency>
- <groupId>mysqlgroupId>
- <artifactId>mysql-connector-javaartifactId>
- <version>8.0.16version>
- dependency>
-
- <dependency>
- <groupId>log4jgroupId>
- <artifactId>log4jartifactId>
- <version>1.2.17version>
- dependency>
- <dependency>
- <groupId>com.github.pagehelpergroupId>
- <artifactId>pagehelperartifactId>
- <version>5.2.0version>
- dependency>
-
- <dependency>
- <groupId>ch.qos.logbackgroupId>
- <artifactId>logback-classicartifactId>
- <version>1.2.3version>
- dependency>
-
- <dependency>
- <groupId>javax.servletgroupId>
- <artifactId>javax.servlet-apiartifactId>
- <version>3.1.0version>
- <scope>providedscope>
- dependency>
- <dependency>
- <groupId>com.fasterxml.jackson.coregroupId>
- <artifactId>jackson-databindartifactId>
- <version>2.12.1version>
- dependency>
-
- <dependency>
- <groupId>commons-fileuploadgroupId>
- <artifactId>commons-fileuploadartifactId>
- <version>1.3.1version>
- dependency>
-
- <dependency>
- <groupId>org.thymeleafgroupId>
- <artifactId>thymeleaf-spring5artifactId>
- <version>3.0.12.RELEASEversion>
- dependency>
- dependencies>
-
- project>
- CREATE TABLE `t_emp` (
- `emp_id` int(11) NOT NULL AUTO_INCREMENT,
- `emp_name` varchar(20) DEFAULT NULL,
- `age` int(11) DEFAULT NULL,
- `sex` char(1) DEFAULT NULL,
- `email` varchar(50) DEFAULT NULL,
- PRIMARY KEY (`emp_id`)
- ) ENGINE=InnoDB DEFAULT CHARSET=utf8
- "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">
-
- <filter>
- <filter-name>CharacterEncodingFilterfilter-name>
- <filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
-
- <init-param>
- <param-name>encodingparam-name>
- <param-value>UTF-8param-value>
- init-param>
- <init-param>
- <param-name>forceEncodingparam-name>
- <param-value>trueparam-value>
- init-param>
- filter>
- <filter-mapping>
- <filter-name>CharacterEncodingFilterfilter-name>
- <url-pattern>/*url-pattern>
- filter-mapping>
-
-
- <filter>
- <filter-name>HiddenHttpMethodFilterfilter-name>
- <filter-class>org.springframework.web.filter.HiddenHttpMethodFilterfilter-class>
- filter>
- <filter-mapping>
- <filter-name>HiddenHttpMethodFilterfilter-name>
- <url-pattern>/*url-pattern>
- filter-mapping>
-
-
- <servlet>
- <servlet-name>SpringMVCservlet-name>
- <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
-
- <init-param>
- <param-name>contextConfigLocationparam-name>
- <param-value>classpath:SpringMVC.xmlparam-value>
- init-param>
-
- <load-on-startup>1load-on-startup>
- servlet>
-
- <servlet-mapping>
- <servlet-name>SpringMVCservlet-name>
- <url-pattern>/url-pattern>
- servlet-mapping>
-
-
- <listener>
- <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
- listener>
-
-
- <context-param>
- <param-name>contextConfigLocationparam-name>
- <param-value>classpath:Spring.xmlparam-value>
- context-param>
- web-app>
- "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-4.2.xsd
- http://www.springframework.org/schema/mvc
- http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-4.2.xsd">
-
- <context:component-scan base-package="com.atguigu.ssm.controller">context:component-scan>
-
-
- <bean id="viewResolver"
- class="org.thymeleaf.spring5.view.ThymeleafViewResolver">
- <property name="order" value="1"/>
- <property name="characterEncoding" value="UTF-8"/>
- <property name="templateEngine">
- <bean class="org.thymeleaf.spring5.SpringTemplateEngine">
- <property name="templateResolver">
- <bean
- class="org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver">
-
- <property name="prefix" value="/WEB-INF/templates/"/>
-
- <property name="suffix" value=".html"/>
- <property name="templateMode" value="HTML5"/>
- <property name="characterEncoding" value="UTF-8" />
- bean>
- property>
- bean>
- property>
- bean>
-
-
- <mvc:default-servlet-handler/>
-
-
- <mvc:annotation-driven/>
-
-
- <mvc:view-controller path="/" view-name="index">mvc:view-controller>
-
-
- <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">bean>
-
-
- <bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">bean>
- beans>
- "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" xmlns:tx="http://www.springframework.org/schema/tx"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
- http://www.springframework.org/schema/mvc
- http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
-
-
- <context:component-scan base-package="com.atguigu.ssm">
-
- <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
- context:component-scan>
-
-
- <context:property-placeholder location="classpath:jdbc.properties">context:property-placeholder>
-
-
- <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
- <property name="driverClassName" value="${jdbc.driver}">property>
- <property name="url" value="${jdbc.url}">property>
- <property name="username" value="${jdbc.username}">property>
- <property name="password" value="${jdbc.password}">property>
- bean>
-
-
- <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
- <property name="dataSource" ref="dataSource">property>
- bean>
-
- <tx:annotation-driven transaction-manager="transactionManager"/>
-
-
- <bean class="org.mybatis.spring.SqlSessionFactoryBean">
-
- <property name="configLocation" value="classpath:mybatis-config.xml">property>
-
- <property name="dataSource" ref="dataSource">property>
-
- <property name="typeAliasesPackage" value="com.atguigu.ssm.pojo">property>
-
-
-
- <property name="plugins">
- <array>
- <bean class="com.github.pagehelper.PageInterceptor">bean>
- array>
- property>
- bean>
-
-
- <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
- <property name="basePackage" value="com.atguigu.ssm.mapper">property>
- bean>
- beans>
- jdbc.driver=com.mysql.cj.jdbc.Driver
- jdbc.url=jdbc:mysql://localhost:3306/ssm?serverTimezone=UTC
- jdbc.username=root
- jdbc.password=123456
- "1.0" encoding="UTF-8" ?>
- configuration
- PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
- "http://mybatis.org/dtd/mybatis-3-config.dtd">
- <configuration>
-
-
-
- <settings>
-
- <setting name="mapUnderscoreToCamelCase" value="true"/>
- settings>
-
-
-
-
-
-
-
-
- configuration>
- "1.0" encoding="UTF-8" ?>
- log4j:configuration SYSTEM "log4j.dtd">
- <log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
- <appender name="STDOUT" class="org.apache.log4j.ConsoleAppender">
- <param name="Encoding" value="UTF-8" />
- <layout class="org.apache.log4j.PatternLayout">
- <param name="ConversionPattern" value="%-5p %d{MM-dd HH:mm:ss,SSS}
- %m (%F:%L) \n" />
- layout>
- appender>
- <logger name="java.sql">
- <level value="debug" />
- logger>
- <logger name="org.apache.ibatis">
- <level value="info" />
- logger>
- <root>
- <level value="debug" />
- <appender-ref ref="STDOUT" />
- root>
- log4j:configuration>
- package com.atguigu.ssm.pojo;
-
- public class Employee {
- private Integer empId;
- private String empName;
- private Integer age;
- private String gender;
- private String email;
-
- public Employee(Integer empId, String empName, Integer age, String gender, String email) {
- this.empId = empId;
- this.empName = empName;
- this.age = age;
- this.gender = gender;
- this.email = email;
- }
-
- public Employee() {
- }
-
- public Integer getEmpId() {
- return empId;
- }
-
- public void setEmpId(Integer empId) {
- this.empId = empId;
- }
-
- public String getEmpName() {
- return empName;
- }
-
- public void setEmpName(String empName) {
- this.empName = empName;
- }
-
- public Integer getAge() {
- return age;
- }
-
- public void setAge(Integer age) {
- this.age = age;
- }
-
- public String getGender() {
- return gender;
- }
-
- public void setGender(String gender) {
- this.gender = gender;
- }
-
- public String getEmail() {
- return email;
- }
-
- public void setEmail(String email) {
- this.email = email;
- }
-
- @Override
- public String toString() {
- return "Employee{" +
- "empId=" + empId +
- ", empName='" + empName + '\'' +
- ", age=" + age +
- ", gender='" + gender + '\'' +
- ", email='" + email + '\'' +
- '}';
- }
- }
- public interface EmployeeService {
- /**
- * 查询所有的员工信息
- * @return
- */
- List
getAllEmployee(); - }
- @Transactional
- @Service
- public class EmployeeServiceImpl implements EmployeeService {
-
- @Autowired
- private EmployeeMapper employeeMapper;
-
- public List
getAllEmployee() { - return employeeMapper.getAllEmployee();
- }
- }
- @Controller
- public class EmployeeController {
- @Autowired
- private EmployeeService employeeService;
-
-
- @RequestMapping(value = "/employee",method = RequestMethod.GET)
- public String getAllEmployee(Model model){
- //查询所有的员工信息
- List
list = employeeService.getAllEmployee(); - //将员工信息在请求域中共享
- model.addAttribute("list",list);
- //跳转到employee_list.html
- return "employee_list";
- }
- }
- html>
- <html lang="en" xmlns:th="http://www.thymeleaf.org">
- <head>
- <meta charset="UTF-8">
- <title>员工列表title>
- <link rel="stylesheet" th:href="@{/static/css/index_work.css}">
- head>
- <body>
- <table>
- <tr>
- <th colspan="6">员工列表th>
- tr>
- <tr>
- <th>流水号th>
- <th>员工姓名th>
- <th>年龄th>
- <th>性别th>
- <th>邮箱th>
- <th>操作th>
- tr>
- <tr th:each="employee,status : ${list}">
- <td th:text="${status.count}">td>
- <td th:text="${employee.empName}">td>
- <td th:text="${employee.age}">td>
- <td th:text="${employee.gender}">td>
- <td th:text="${employee.email}">td>
- <td>
- <a th:href="">删除a>
- <a th:href="">修改a>
- td>
- tr>
- table>
- body>
- html>
结果:
- /**
- * 获取员工的分页信息
- * @param pageNum
- * @return
- */
- PageInfo
getEmployeePage(Integer pageNum);
- @RequestMapping(value = "/employee/page/{pageNum}",method = RequestMethod.GET)
- public String getEmployeePage(@PathVariable("pageNum") Integer pageNum,Model model){
- //获取员工的分页信息
- PageInfo
page = employeeService.getEmployeePage(pageNum); - //将分页数据共享到请求域中
- model.addAttribute("page",page);
- //跳转到employee_list.html
- return "employee_list";
- }
- public PageInfo
getEmployeePage(Integer pageNum) { - //开启分页功能
- PageHelper.startPage(pageNum,4);
- //查询所有的员工信息
- List
list = employeeMapper.getAllEmployee(); - //获取分页相关数据
- PageInfo
page = new PageInfo(list,5); - return page;
- }
- html>
- <html lang="en" xmlns:th="http://www.thymeleaf.org" xmlns:color="http://www.w3.org/1999/xhtml">
- <head>
- <meta charset="UTF-8">
- <title>员工列表title>
- <link rel="stylesheet" th:href="@{/static/css/index_work.css}">
- head>
- <body>
- <table>
- <tr>
- <th colspan="6">员工列表th>
- tr>
- <tr>
- <th>流水号th>
- <th>员工姓名th>
- <th>年龄th>
- <th>性别th>
- <th>邮箱th>
- <th>操作th>
- tr>
- <tr th:each="employee,status : ${page.list}">
- <td th:text="${status.count}">td>
- <td th:text="${employee.empName}">td>
- <td th:text="${employee.age}">td>
- <td th:text="${employee.gender}">td>
- <td th:text="${employee.email}">td>
- <td>
- <a th:href="">删除a>
- <a th:href="">修改a>
- td>
- tr>
- table>
- <div style="text-align: center">
- <a th:if="${page.hasPreviousPage}" th:href="@{/employee/page/1}">首页a>
- <a th:if="${page.hasPreviousPage}" th:href="@{'/employee/page' + ${page.prePage}}">上一页a>
-
- <span th:each="num : ${page.navigatepageNums}">
- <a th:if="${page.pageNum == num} style="color: red;" th:href="@{'/employee/page' + ${num}}" th:text="'['+${num}+']'">a>
- <a th:if="${page.pageNum != num} th:href="@{'/employee/page' + ${num}}" th:text="${num}">下一页a>
- span>
-
- <a th:if="${page.hasNextPage}" th:href="@{'/employee/page' + ${page.nextPage}}">下一页a>
- <a th:if="${page.hasNextPage}" th:href="@{'/employee/page' + ${page.pages}}">末页a>
- div>
- body>
- html>