Java之Spring mvc详解(非原创)

文章大纲

一、Spring mvc介绍
二、Spring mvc代码实战
三、项目源码下载
四、参考文章

 

一、Spring mvc介绍

1. 什么是springmvc

  springmvc是spring框架的一个模块,springmvc和spring无需通过中间整合层进行整合。springmvc是一个基于mvc的web框架。

 

2. mvc设计模式在b/s系统 下的应用

 

3. Spring mvc框架执行流程

 

  第一步:发起请求到前端控制器(DispatcherServlet)
  第二步:前端控制器请求HandlerMapping查找 Handler,可以根据xml配置、注解进行查找,通过@RequestMapping(value = "/test")中的test进行查找
  第三步:处理器映射器HandlerMapping向前端控制器返回Handler
  第四步:前端控制器调用处理器适配器去执行Handler
  第五步:处理器适配器去执行Handler
  第六步:Handler执行完成给适配器返回ModelAndView
  第七步:处理器适配器向前端控制器返回ModelAndView,ModelAndView是springmvc框架的一个底层对象,包括 Model和view
  第八步:前端控制器请求视图解析器去进行视图解析,根据逻辑视图名解析成真正的视图(jsp)
  第九步:视图解析器向前端控制器返回View
  第十步:前端控制器进行视图渲染,视图渲染将模型数据(在ModelAndView对象中)填充到request域
  第十一步:前端控制器向用户响应结果

4. Spring mvc组件介绍

(1)前端控制器DispatcherServlet(不需要程序员开发)
作用接收请求,响应结果,相当于转发器,中央处理器。
有了DispatcherServlet减少了其它组件之间的耦合度。

(2)处理器映射器HandlerMapping(不需要程序员开发)
作用:根据请求的url查找Handler

(3)处理器适配器HandlerAdapter
作用:按照特定规则(HandlerAdapter要求的规则)去执行Handler

(4)处理器Handler(需要程序员开发)
注意:编写Handler时按照HandlerAdapter的要求去做,这样适配器才可以去正确执行Handler

(5)视图解析器View resolver(不需要程序员开发)
作用:进行视图解析,根据逻辑视图名解析成真正的视图(view)

(6)视图View(需要程序员开发jsp)
View是一个接口,实现类支持不同的View类型(jsp、freemarker、pdf...)

二、Spring mvc代码实战

  Spring mvc常见使用功能有数据交互方式(ModelAndView和JSON)、静态资源的解析、参数校验、全局异常处理、拦截器、上传图片等。

1. 创建maven的javaweb项目

文章重点在于讲解Spring mvc功能,因此创建项目方式不进行深入讲解,创建后的项目目录如下:

 

2. Spring mvc基本配置

2.1 在pom.xml添加maven依赖

<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.0</modelVersion>
  <groupId>springmvc_demo</groupId>
  <artifactId>springmvc_demo</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  <name/>
  <description/>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <!-- spring版本号 -->
    <spring.version>4.2.5.RELEASE</spring.version>

  </properties>

  <dependencies>

    <dependency>
      <groupId>com.google.code.gson</groupId>
      <artifactId>gson</artifactId>
      <version>2.8.2</version>
    </dependency>


    <dependency>
      <groupId>org.apache.openejb</groupId>
      <artifactId>javaee-api</artifactId>
      <version>5.0-1</version>
      <scope>provided</scope>
    </dependency>
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>jstl</artifactId>
      <version>1.2</version>
      <scope>provided</scope>
    </dependency>
    <dependency>
      <groupId>javax.servlet.jsp</groupId>
      <artifactId>jsp-api</artifactId>
      <version>2.1</version>
      <scope>provided</scope>
    </dependency>
    <!-- 分页 -->
    <dependency>
      <groupId>com.github.pagehelper</groupId>
      <artifactId>pagehelper</artifactId>
      <version>4.1.4</version>
    </dependency>

    <!--测试包-->
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>

    <!-- c3p0数据库连接池 -->
    <dependency>
      <groupId>c3p0</groupId>
      <artifactId>c3p0</artifactId>
      <version>0.9.1.2</version>
    </dependency>

    <!-- commons工具包 -->

    <!--图片上传相关的-->
    <dependency>
      <groupId>commons-fileupload</groupId>
      <artifactId>commons-fileupload</artifactId>
      <version>1.3.1</version>
    </dependency>
    <dependency>
      <groupId>commons-io</groupId>
      <artifactId>commons-io</artifactId>
      <version>2.6</version>
    </dependency>


    <dependency>
      <groupId>commons-beanutils</groupId>
      <artifactId>commons-beanutils</artifactId>
      <version>1.7.0</version>
    </dependency>
    <dependency>
      <groupId>commons-codec</groupId>
      <artifactId>commons-codec</artifactId>
      <version>1.7</version>
    </dependency>
    
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章