第一章:本质

  • ContextLoaderListener:读取 spring-persist.xml。
  • DispatcherServlet:读取 springmvc.xml。

第二章:web.xml 配置

  • web.xml
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  5. http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
  6. version="4.0">
  7. <!-- ContextLoaderListener -->
  8. <!-- 通过 context-param 指定 Spring 框架的配置文件位置 -->
  9. <context-param>
  10. <param-name>contextConfigLocation</param-name>
  11. <param-value>classpath:spring-persist.xml</param-value>
  12. </context-param>
  13. <!-- 配置 ContextLoaderListener 监听器 -->
  14. <listener>
  15. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  16. </listener>
  17. <!-- DispatcherServlet -->
  18. <servlet>
  19. <servlet-name>dispatcherServlet</servlet-name>
  20. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  21. <init-param>
  22. <param-name>contextConfigLocation</param-name>
  23. <param-value>classpath:springmvc.xml</param-value>
  24. </init-param>
  25. <load-on-startup>1</load-on-startup>
  26. </servlet>
  27. <servlet-mapping>
  28. <servlet-name>dispatcherServlet</servlet-name>
  29. <url-pattern>/</url-pattern>
  30. </servlet-mapping>
  31. <!-- 需要注意两个 Filter 的顺序:字符集过滤器在前,转换请求方式过滤器在后 -->
  32. <!-- CharacterEncodingFilter -->
  33. <filter>
  34. <filter-name>characterEncodingFilter</filter-name>
  35. <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
  36. <init-param>
  37. <param-name>encoding</param-name>
  38. <param-value>UTF-8</param-value>
  39. </init-param>
  40. <init-param>
  41. <param-name>forceRequestEncoding</param-name>
  42. <param-value>true</param-value>
  43. </init-param>
  44. <init-param>
  45. <param-name>forceResponseEncoding</param-name>
  46. <param-value>true</param-value>
  47. </init-param>
  48. </filter>
  49. <filter-mapping>
  50. <filter-name>characterEncodingFilter</filter-name>
  51. <url-pattern>/*</url-pattern>
  52. </filter-mapping>
  53. <!-- HiddenHttpMethodFilter -->
  54. <filter>
  55. <filter-name>hiddenHttpMethodFilter</filter-name>
  56. <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
  57. </filter>
  58. <filter-mapping>
  59. <filter-name>hiddenHttpMethodFilter</filter-name>
  60. <url-pattern>/*</url-pattern>
  61. </filter-mapping>
  62. </web-app>

第三章:SpringMVC 配置

  • springmvc.xml
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
  6. <!-- SpringMVC 只扫描 handler 类所在的包 -->
  7. <!-- Spring 和 SpringMVC 扫描各自负责的组件,扫描的范围没有重合的部分,直接避免了重复创建对象 -->
  8. <context:component-scan base-package="com.github.fairy.era.handler"/>
  9. <!-- 配置 Thymeleaf 的视图解析器 -->
  10. <bean id="thymeleafViewResolver" class="org.thymeleaf.spring5.view.ThymeleafViewResolver">
  11. <property name="order" value="1"/>
  12. <property name="characterEncoding" value="UTF-8"/>
  13. <property name="templateEngine">
  14. <bean class="org.thymeleaf.spring5.SpringTemplateEngine">
  15. <property name="templateResolver">
  16. <bean class="org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver">
  17. <property name="prefix" value="/WEB-INF/templates/"/>
  18. <property name="suffix" value=".html"/>
  19. <property name="characterEncoding" value="UTF-8"/>
  20. <property name="templateMode" value="HTML5"/>
  21. </bean>
  22. </property>
  23. </bean>
  24. </property>
  25. </bean>
  26. <!-- SpringMVC 注解驱动(标配) -->
  27. <mvc:annotation-driven/>
  28. <!-- 对于没有映射的请求直接转发放行,主要是静态资源 -->
  29. <mvc:default-servlet-handler/>
  30. <!-- 匹配请求路径直接前往视图,不经过 handler 方法 -->
  31. <mvc:view-controller path="/" view-name="portal"/>
  32. <mvc:view-controller path="/index.html" view-name="portal"/>
  33. </beans>

第四章:创建组件

  • EmployeeHandler.java
  1. package com.github.fairy.era.handler;
  2. import com.github.fairy.era.entity.Employee;
  3. import com.github.fairy.era.service.EmployeeService;
  4. import lombok.RequiredArgsConstructor;
  5. import org.springframework.stereotype.Controller;
  6. import org.springframework.ui.Model;
  7. import org.springframework.web.bind.annotation.GetMapping;
  8. import java.util.List;
  9. /**
  10. * @author 许大仙
  11. * @version 1.0
  12. * @since 2021-11-22 13:48
  13. */
  14. @Controller
  15. @RequiredArgsConstructor
  16. public class EmployeeHandler {
  17. private final EmployeeService employeeService;
  18. @GetMapping("/get/all")
  19. public String getAll(Model model) {
  20. // 1、查询数据
  21. List<Employee> empList = employeeService.findAll();
  22. // 2.存入模型
  23. model.addAttribute("empList", empList);
  24. return "emp-list";
  25. }
  26. }

第五章:页面操作

5.1 首页超链接

  • portal.html
  1. <!DOCTYPE html>
  2. <html lang="en" xmlns:th="http://www.thymeleaf.org">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Title</title>
  6. </head>
  7. <body>
  8. <a th:href="@{/get/all}">显示全部数据</a>
  9. </body>
  10. </html>

5.2 显示数据的页面

  • emp-list.html
  1. <!DOCTYPE html>
  2. <html lang="en" xmlns:th="http://www.thymeleaf.org">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>$Title$</title>
  6. </head>
  7. <body>
  8. <table>
  9. <tr>
  10. <th>id</th>
  11. <th>lastName</th>
  12. <th>gender</th>
  13. <th>email</th>
  14. </tr>
  15. <tbody th:if="${#lists.isEmpty(empList)}">
  16. <tr>
  17. <td colspan="3">抱歉!没有查询到数据!</td>
  18. </tr>
  19. </tbody>
  20. <tbody th:if="${not #lists.isEmpty(empList)}">
  21. <tr th:each="emp : ${empList}">
  22. <td th:text="${emp.id}">这里显示员工ID</td>
  23. <td th:text="${emp.lastName}">这里显示员工lastName</td>
  24. <td th:text="${emp.gender}">这里显示员工gender</td>
  25. <td th:text="${emp.email}">这里显示员工email</td>
  26. </tr>
  27. </tbody>
  28. </table>
  29. <a th:href="@{/}">回首页</a>
  30. </body>
  31. </html>