两个入门案例带你入门SpringMVC 注解版&&XML版

举报
陶然同学 发表于 2022/06/25 00:15:14 2022/06/25
【摘要】 目录 概述 执行流程 XML版         搭建环境         步骤         实现 注解版         搭建环境   &nb...

目录

概述

执行流程

XML版

        搭建环境

        步骤

        实现

注解版

        搭建环境

        步骤

        实现


概述

  • Spring MVC 是 Spring 提供的一个基于 MVC 设计模式的轻量级 Web 开发框架,本质上相当于 Servlet。

  • 由于 Spring MVC 本身就是 Spring 框架的一部分,所有与 Spring 框架是无缝集成。是当今业界最主流的 Web 开发框架。

执行流程

  1. 用户点击某个请求路径,发起一个 HTTP request 请求,该请求会被提交到 DispatcherServlet(前端控制器);

  2. 由 DispatcherServlet 请求一个或多个 HandlerMapping(处理器映射器),并返回一个执行链(HandlerExecutionChain)。

  3. DispatcherServlet 将执行链返回的 Handler 信息发送给 HandlerAdapter(处理器适配器);

  4. HandlerAdapter 根据 Handler 信息找到并执行相应的 Handler(常称为 Controller);

  5. Handler 执行完毕后会返回给 HandlerAdapter 一个 ModelAndView 对象(Spring MVC的底层对象,包括 Model 数据模型和 View 视图信息);

  6. HandlerAdapter 接收到 ModelAndView 对象后,将其返回给 DispatcherServlet ;

  7. DispatcherServlet 接收到 ModelAndView 对象后,会请求 ViewResolver(视图解析器)对视图进行解析;

  8. ViewResolver 根据 View 信息匹配到相应的视图结果,并返回给 DispatcherServlet;

  9. DispatcherServlet 接收到具体的 View 视图后,进行视图渲染,将 Model 中的模型数据填充到 View 视图中的 request 域,生成最终的 View(视图);

  10. 视图负责将结果显示到浏览器(客户端)。

XML

        搭建环境

                导入所需jar包坐标


  
  1. <packaging>war</packaging>
  2. <properties>
  3. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  4. <maven.compiler.source>1.8</maven.compiler.source>
  5. <maven.compiler.target>1.8</maven.compiler.target>
  6. </properties>
  7. <dependencies>
  8. <dependency>
  9. <groupId>org.springframework</groupId>
  10. <artifactId>spring-context</artifactId>
  11. <version>5.2.4.RELEASE</version>
  12. </dependency>
  13. <dependency>
  14. <groupId>org.springframework</groupId>
  15. <artifactId>spring-aspects</artifactId>
  16. <version>5.2.4.RELEASE</version>
  17. </dependency>
  18. <dependency>
  19. <groupId>org.springframework</groupId>
  20. <artifactId>spring-test</artifactId>
  21. <version>5.2.4.RELEASE</version>
  22. </dependency>
  23. <dependency>
  24. <groupId>org.springframework</groupId>
  25. <artifactId>spring-webmvc</artifactId>
  26. <version>5.2.4.RELEASE</version>
  27. </dependency>
  28. <!-- 事务 -->
  29. <dependency>
  30. <groupId>org.springframework</groupId>
  31. <artifactId>spring-jdbc</artifactId>
  32. <version>5.2.4.RELEASE</version>
  33. </dependency>
  34. <!-- mysql驱动 -->
  35. <dependency>
  36. <groupId>mysql</groupId>
  37. <artifactId>mysql-connector-java</artifactId>
  38. <version>8.0.24</version>
  39. </dependency>
  40. <dependency>
  41. <groupId>com.alibaba</groupId>
  42. <artifactId>druid</artifactId>
  43. <version>1.2.4</version>
  44. </dependency>
  45. <dependency>
  46. <groupId>org.mybatis</groupId>
  47. <artifactId>mybatis</artifactId>
  48. <version>3.5.7</version>
  49. </dependency>
  50. <dependency>
  51. <groupId>tk.mybatis</groupId>
  52. <artifactId>mapper</artifactId>
  53. <version>3.5.2</version>
  54. </dependency>
  55. <dependency>
  56. <groupId>com.github.pagehelper</groupId>
  57. <artifactId>pagehelper</artifactId>
  58. <version>3.7.5</version>
  59. </dependency>
  60. <!--整合-->
  61. <dependency>
  62. <groupId>org.mybatis</groupId>
  63. <artifactId>mybatis-spring</artifactId>
  64. <version>2.0.5</version>
  65. </dependency>
  66. <dependency>
  67. <groupId>junit</groupId>
  68. <artifactId>junit</artifactId>
  69. <version>4.12</version>
  70. </dependency>
  71. <!-- mvc json -->
  72. <dependency>
  73. <groupId>com.fasterxml.jackson.core</groupId>
  74. <artifactId>jackson-databind</artifactId>
  75. <version>2.10.2</version>
  76. </dependency>
  77. <!--swagger2-->
  78. <dependency>
  79. <groupId>io.springfox</groupId>
  80. <artifactId>springfox-swagger2</artifactId>
  81. <version>2.7.0</version>
  82. </dependency>
  83. <dependency>
  84. <groupId>io.springfox</groupId>
  85. <artifactId>springfox-swagger-ui</artifactId>
  86. <version>2.7.0</version>
  87. </dependency>
  88. <!--文件上传-->
  89. <dependency>
  90. <groupId>commons-fileupload</groupId>
  91. <artifactId>commons-fileupload</artifactId>
  92. <version>1.4</version>
  93. </dependency>
  94. <!--jsp相关-->
  95. <dependency>
  96. <groupId>javax.servlet</groupId>
  97. <artifactId>javax.servlet-api</artifactId>
  98. <version>3.1.0</version>
  99. <scope>provided</scope>
  100. </dependency>
  101. <dependency>
  102. <groupId>javax.servlet.jsp</groupId>
  103. <artifactId>javax.servlet.jsp-api</artifactId>
  104. <version>2.3.1</version>
  105. <scope>provided</scope>
  106. </dependency>
  107. <dependency>
  108. <groupId>javax.servlet</groupId>
  109. <artifactId>jstl</artifactId>
  110. <version>1.2</version>
  111. </dependency>
  112. <dependency>
  113. <groupId>org.slf4j</groupId>
  114. <artifactId>slf4j-log4j12</artifactId>
  115. <version>1.7.10</version>
  116. </dependency>
  117. </dependencies>

        步骤

                1.编写Controller UserController

                2.编写jsp页面 

                3.编写配置文件扫描Controller

                4.在web.xml中配置前端控制器

                5.编写首页 部署项目 并访问

        实现

                1.编写Controller UserController


  
  1. @Controller
  2. @RequestMapping("/user")
  3. public class UserController {
  4. @RequestMapping("/selectAll")
  5. public String selectAll(){
  6. return "/list.jsp";
  7. }
  8. }

                2.编写jsp页面


  
  1. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
  2. <html>
  3. <head>
  4. <title>Title</title>
  5. </head>
  6. <body>
  7. 列表页面
  8. </body>
  9. </html>

                3.编写springmvc.xml配置文件 扫描Controller


  
  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:p="http://www.springframework.org/schema/p"
  5. xmlns:context="http://www.springframework.org/schema/context"
  6. xmlns:aop="http://www.springframework.org/schema/aop"
  7. xmlns:mvc="http://www.springframework.org/schema/mvc"
  8. xsi:schemaLocation="http://www.springframework.org/schema/beans
  9. http://www.springframework.org/schema/beans/spring-beans.xsd
  10. http://www.springframework.org/schema/mvc
  11. http://www.springframework.org/schema/mvc/spring-mvc.xsd
  12. http://www.springframework.org/schema/aop
  13. http://www.springframework.org/schema/aop/spring-aop.xsd
  14. http://www.springframework.org/schema/context
  15. http://www.springframework.org/schema/context/spring-context.xsd">
  16. <context:component-scan base-package="com.czxy.mvc.controller"></context:component-scan>
  17. </beans>

                4.在web.xml配置前端控制器


  
  1. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  4. http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
  5. version="4.0">
  6. <display-name>Archetype Created Web Application</display-name>
  7. <servlet>
  8. <servlet-name>springmvc</servlet-name>
  9. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  10. <init-param>
  11. <param-name>contextConfigLocation</param-name>
  12. <param-value>classpath:springmvc.xml</param-value>
  13. </init-param>
  14. </servlet>
  15. <servlet-mapping>
  16. <servlet-name>springmvc</servlet-name>
  17. <url-pattern>*.action</url-pattern>
  18. </servlet-mapping>
  19. </web-app>

                5.编写首页 部署项目并访问

注解版

        搭建环境

        导入所需jar包坐标


  
  1. <packaging>war</packaging>
  2. <properties>
  3. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  4. <maven.compiler.source>1.8</maven.compiler.source>
  5. <maven.compiler.target>1.8</maven.compiler.target>
  6. <!--声明不需要web.xml文件-->
  7. <failOnMissingWebXml>false</failOnMissingWebXml>
  8. </properties>
  9. <dependencies>
  10. <dependency>
  11. <groupId>org.springframework</groupId>
  12. <artifactId>spring-context</artifactId>
  13. <version>5.2.4.RELEASE</version>
  14. </dependency>
  15. <dependency>
  16. <groupId>org.springframework</groupId>
  17. <artifactId>spring-aspects</artifactId>
  18. <version>5.2.4.RELEASE</version>
  19. </dependency>
  20. <dependency>
  21. <groupId>org.springframework</groupId>
  22. <artifactId>spring-test</artifactId>
  23. <version>5.2.4.RELEASE</version>
  24. </dependency>
  25. <dependency>
  26. <groupId>org.springframework</groupId>
  27. <artifactId>spring-webmvc</artifactId>
  28. <version>5.2.4.RELEASE</version>
  29. </dependency>
  30. <!-- 事务 -->
  31. <dependency>
  32. <groupId>org.springframework</groupId>
  33. <artifactId>spring-jdbc</artifactId>
  34. <version>5.2.4.RELEASE</version>
  35. </dependency>
  36. <!-- mysql驱动 -->
  37. <dependency>
  38. <groupId>mysql</groupId>
  39. <artifactId>mysql-connector-java</artifactId>
  40. <version>8.0.24</version>
  41. </dependency>
  42. <dependency>
  43. <groupId>com.alibaba</groupId>
  44. <artifactId>druid</artifactId>
  45. <version>1.2.4</version>
  46. </dependency>
  47. <dependency>
  48. <groupId>org.mybatis</groupId>
  49. <artifactId>mybatis</artifactId>
  50. <version>3.5.7</version>
  51. </dependency>
  52. <dependency>
  53. <groupId>tk.mybatis</groupId>
  54. <artifactId>mapper</artifactId>
  55. <version>3.5.2</version>
  56. </dependency>
  57. <dependency>
  58. <groupId>com.github.pagehelper</groupId>
  59. <artifactId>pagehelper</artifactId>
  60. <version>3.7.5</version>
  61. </dependency>
  62. <!--整合-->
  63. <dependency>
  64. <groupId>org.mybatis</groupId>
  65. <artifactId>mybatis-spring</artifactId>
  66. <version>2.0.5</version>
  67. </dependency>
  68. <dependency>
  69. <groupId>junit</groupId>
  70. <artifactId>junit</artifactId>
  71. <version>4.12</version>
  72. </dependency>
  73. <!-- mvc json -->
  74. <dependency>
  75. <groupId>com.fasterxml.jackson.core</groupId>
  76. <artifactId>jackson-databind</artifactId>
  77. <version>2.10.2</version>
  78. </dependency>
  79. <!--swagger2-->
  80. <dependency>
  81. <groupId>io.springfox</groupId>
  82. <artifactId>springfox-swagger2</artifactId>
  83. <version>2.7.0</version>
  84. </dependency>
  85. <dependency>
  86. <groupId>io.springfox</groupId>
  87. <artifactId>springfox-swagger-ui</artifactId>
  88. <version>2.7.0</version>
  89. </dependency>
  90. <!--文件上传-->
  91. <dependency>
  92. <groupId>commons-fileupload</groupId>
  93. <artifactId>commons-fileupload</artifactId>
  94. <version>1.4</version>
  95. </dependency>
  96. <!--jsp相关-->
  97. <dependency>
  98. <groupId>javax.servlet</groupId>
  99. <artifactId>javax.servlet-api</artifactId>
  100. <version>3.1.0</version>
  101. <scope>provided</scope>
  102. </dependency>
  103. <dependency>
  104. <groupId>javax.servlet.jsp</groupId>
  105. <artifactId>javax.servlet.jsp-api</artifactId>
  106. <version>2.3.1</version>
  107. <scope>provided</scope>
  108. </dependency>
  109. <dependency>
  110. <groupId>javax.servlet</groupId>
  111. <artifactId>jstl</artifactId>
  112. <version>1.2</version>
  113. </dependency>
  114. <dependency>
  115. <groupId>org.slf4j</groupId>
  116. <artifactId>slf4j-log4j12</artifactId>
  117. <version>1.7.10</version>
  118. </dependency>
  119. </dependencies>

        步骤

                1.编写Controller UserController

                2.编写jsp 转发到jsp

                3.编写核心配置类(替代xml版 springconfig.xml)

                4.编写核心配置类(web.xm)

                5.编写首页 部署 并访问http://localhost:8080/maven_mvc_anno/user/selectAll.action

        实现

                1.编写Controller UserController


  
  1. @Controller
  2. @RequestMapping("/user")
  3. public class UserController {
  4. @RequestMapping("/selectAll")
  5. public String selectAll(){
  6. return "/list.jsp";
  7. }
  8. }

                2.编写jsp 显示提示 list.jsp


  
  1. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
  2. <html>
  3. <head>
  4. <title>Title</title>
  5. </head>
  6. <body>
  7. 列表
  8. </body>
  9. </html>

                3.编写配置类 MvcConfiguration1 扫描Controller


  
  1. @Configuration
  2. @ComponentScan(basePackages = "com.czxy.controller")
  3. public class MvcConfiguration1 {
  4. }

                4.编写WebInitializer 配置类


  
  1. public class WebInitializer1 implements WebApplicationInitializer {
  2. @Override
  3. public void onStartup(ServletContext servletContext) throws ServletException {
  4. //1 配置spring工厂
  5. AnnotationConfigWebApplicationContext application = new AnnotationConfigWebApplicationContext();
  6. // 注册所有的配置类
  7. application.register(MvcConfiguration1.class);
  8. //2 post中文乱码
  9. FilterRegistration.Dynamic encodingFilter = servletContext.addFilter("encoding", new CharacterEncodingFilter("UTF-8"));
  10. encodingFilter.addMappingForUrlPatterns(null, true, "/*");
  11. //3 核心控制器
  12. ServletRegistration.Dynamic mvcServlet = servletContext.addServlet("springmvc", new DispatcherServlet(application));
  13. mvcServlet.addMapping("*.action");
  14. mvcServlet.setLoadOnStartup(2); //tomcat启动时,执行servlet的初始化方法
  15. }
  16. }

                5.编写首页 index.jsp


  
  1. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
  2. <html>
  3. <head>
  4. <title>Title</title>
  5. </head>
  6. <body>
  7. <a href="${pageContext.request.contextPath}/user/selectAll.action">入门案例</a>
  8. </body>
  9. </html>

               6.部署并启动服务器 访问

文章来源: blog.csdn.net,作者:陶然同学,版权归原作者所有,如需转载,请联系作者。

原文链接:blog.csdn.net/weixin_45481821/article/details/121791760

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。