• 01 SpringMVC 入门


    目录

    一、SpringMVC简介

    二、SpringMVC入门案例

    1.使用Maven创建一个web项目,补齐包结构。

    2.引入相关依赖和tomcat插件

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

    4.编写SpringMVC核心配置文件springmvc.xml,该配置文件和spring配置文件写法一样

    5.编写控制器

    6.使用tomcat插件启动项目,访问http:/localhost:8080/c1/hello1

    三、SpringMVC执行流程

     1.SpringMVC的组件

    2.组件的工作流程

    四、知识点整理

    一、SpringMVC简介

    MVC 模型
    MVC全称Model View Controller 是一种设计创建 Web 应用程序的模式。这三个单词分别代表Web 应用程序的三个部分:
    Model (模型):指数据模型。用于存储数据以及处理用户请求的业务逻辑。在Web 应用中, JavaBean 对象,业务模型等都属于Model
    View (视图):用于展示模型中的数据的,一般为 jsp html 文件。
    Controller (控制器):是应用程序中处理用户交互的部分。接受视图提出的请求,将数据交给模型处理,并将处理后的结果交 给视图显示。

    SpringMVC
    SpringMVC 是一个基于 MVC 模式的轻量级 Web 框架,是 Spring 框架的一个模块,和Spring 可以直接整合使用。 SpringMVC 代替了Servlet技术,它通过一套注解,让一个简单的 Java 类成为处理请求的控制器,而无须实 现任何接口。

    二、SpringMVC入门案例

     接下来我们编写一个SpringMVC的入门案例

    1.使用Maven创建一个web项目,补齐包结构。

    2.引入相关依赖和tomcat插件

    1. <dependencies>
    2. <dependency>
    3. <groupId>org.springframeworkgroupId>
    4. <artifactId>spring-contextartifactId>
    5. <version>5.2.12.RELEASEversion>
    6. dependency>
    7. <dependency>
    8. <groupId>org.springframeworkgroupId>
    9. <artifactId>spring-webartifactId>
    10. <version>5.2.12.RELEASEversion>
    11. dependency>
    12. <dependency>
    13. <groupId>org.springframeworkgroupId>
    14. <artifactId>spring-webmvcartifactId>
    15. <version>5.2.12.RELEASEversion>
    16. dependency>
    17. <dependency>
    18. <groupId>javax.servletgroupId>
    19. <artifactId>servlet-apiartifactId>
    20. <version>2.5version>
    21. <scope>providedscope>
    22. dependency>
    23. <dependency>
    24. <groupId>javax.servlet.jspgroupId>
    25. <artifactId>jsp-apiartifactId>
    26. <version>2.0version>
    27. <scope>providedscope>
    28. dependency>
    29. <dependency>
    30. <groupId>com.fasterxml.jackson.coregroupId>
    31. <artifactId>jackson-coreartifactId>
    32. <version>2.9.0version>
    33. dependency>
    34. <dependency>
    35. <groupId>com.fasterxml.jackson.coregroupId>
    36. <artifactId>jackson-databindartifactId>
    37. <version>2.9.0version>
    38. dependency>
    39. <dependency>
    40. <groupId>com.fasterxml.jackson.coregroupId>
    41. <artifactId>jackson-annotationsartifactId>
    42. <version>2.9.0version>
    43. dependency>
    44. dependencies>
    45. <build>
    46. <plugins>
    47. <plugin>
    48. <groupId>org.apache.tomcat.mavengroupId>
    49. <artifactId>tomcat7-maven-pluginartifactId>
    50. <version>2.1version>
    51. <configuration>
    52. <port>8080port>
    53. <path>/path>
    54. <uriEncoding>UTF-8uriEncoding>
    55. <server>tomcat7server>
    56. <systemProperties>
    57. <java.util.logging.SimpleFormatter.format>%1$tH:%1$tM:%1$tS %2$s%n%4$s: %5$s%6$s%n java.util.logging.SimpleFormatter.format>
    58. systemProperties>
    59. configuration>
    60. plugin>
    61. plugins>
    62. build>

    3.在web.xml中配置前端控制DispatcherServlet

    1. <web-app>
    2. <display-name>Archetype Created Web Applicationdisplay-name>
    3. <servlet>
    4. <servlet-name>dispatcherServletservlet-name>
    5. <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
    6. <init-param>
    7. <param-name>contextConfigLocationparam-name>
    8. <param-value>classpath:springmvc.xmlparam-value>
    9. init-param>
    10. <load-on-startup>1load-on-startup>
    11. servlet>
    12. <servlet-mapping>
    13. <servlet-name>dispatcherServletservlet-name>
    14. <url-pattern>/url-pattern>
    15. servlet-mapping>
    16. web-app>

    4.编写SpringMVC核心配置文件springmvc.xml,该配置文件和spring配置文件写法一样

    1. <beans xmlns="http://www.springframework.org/schema/beans"
    2. xmlns:mvc="http://www.springframework.org/schema/mvc"
    3. xmlns:context="http://www.springframework.org/schema/context"
    4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    5. xsi:schemaLocation=" http://www.springframework.org/schema/beans
    6. http://www.springframework.org/schema/beans/spring-beans.xsd
    7. http://www.springframework.org/schema/mvc
    8. http://www.springframework.org/schema/mvc/spring-mvc.xsd
    9. http://www.springframework.org/schema/context
    10. http://www.springframework.org/schema/context/spring-context.xsd">
    11. <context:component-scan base-package="com.itbaizhan">context:component-scan>
    12. <mvc:annotation-driven>mvc:annotation-driven>
    13. beans>

    5.编写控制器

    1. @Controller
    2. public class MyController1 {
    3. // 该控制器的访问路径为/c1/hello1
    4. @RequestMapping("/c1/hello1")
    5. public void helloMVC(){
    6. System.out.println("hello,SpringMVC");
    7. }
    8. }

    6.使用tomcat插件启动项目,访问http:/localhost:8080/c1/hello1

    三、SpringMVC执行流程

     1.SpringMVC的组件

    DispatcherServlet:前端控制器,接受所有请求,调用其他组件,相当于转发器

    HandlerMapping:处理器映射器,根据配置找到方法的执行链

    HandlerAdapter:处理器适配器,根据方法类型找到对应的处理器

    Handler:处理器,需要程序员开发

    ViewResolver:视图解析器,找到指定试图

    视图View:View是一个接口, 它的实现类支持不同的视图类型,如jsp,freemarker,pdf等等

    2.SpringMVC的工作流程

    (1)客户端将请求发送给前端控制器DispatcherServlet

    (2)前端控制器收到请求后,将请求发送给处理器映射器HandlerMapping,处理器映射器根据路径找到方法的执行链Handler,返回给前端控制器DispatcherServlet

    (3)前端控制器将方法的执行链Handler发送给处理器适配器HandlerAdapter,处理器适配器HandlerAdapter根据方法类型找到对应的处理器

    (4)处理器执行方法,将结果返回给前端控制器

    (5)前端控制器将结果发送给视图解析器ViewResolver,视图解析器找到视图文件位置并进行解析

    (6)视图解析器ViewResolver解析完成后返回具体的视图View,然后前端控制器DispatcherServletView进行渲染视图

    (7)最后前端控制器DispatcherServlet将结果显示到客户端

    四、知识点整理

    1.在MVC模型中,Controller指的是“应用程序中处理用户交互的部分”

    2.SpringMVC框架可以让一个简单JAVA类成为“控制器”、“视图”、“模型”

    3.使用SpringMVC必须要配置的是“前端控制器”

    4.在SpringMVC中,通过“视图解析器”组件找到指定视图

    5.SpringMVC中,通过“前端控制器”组件接受所有请求,调用其他组件

  • 相关阅读:
    Netty 如何高效接收网络数据?一文聊透 ByteBuffer 动态自适应扩缩容机制
    智慧物流之道:数据可视化引领全局监控
    vue uniapp 实现点击获取坐标出现gif
    vue多条件查询
    OA项目(一)之用户管理[登录+分页查询]
    对极几何与三角化求3D空间坐标
    112. 路径总和
    <<Java>> Thread 类的基本用法
    在pandas中使用query替代loc进行高效简洁的条件筛选
    django+django-haystack+Whoosh(后期切换引擎为Elasticsearch+ik)+Jieba+mysql
  • 原文地址:https://blog.csdn.net/m0_51697147/article/details/126251219