1 什么是SpringMVC

  是一个mvc框架,用来简化基于mvc架构的web应用程序的 开发。

2 SpringMVC五大组件

  DispatcherServlet (前端控制器)

  HanlderMapping

  Controller (处理器)

  ModelAndView

  ViewResolver (视图解析器)

  2.1 springMVC 的五大组件怎么协同工作

    》DispatcherServlet收到请求之后,依据 HandlerMapping的配置调用相应的处理器来处理。

    》处理器将处理结果封装成ModelAndView, 然后返回给DispatcherServlet。

    》DispatcherServlet依据ViewResovler的 解析,调用相应的视图对象(比如某个jsp)来生成 相应的页面。

3 SpringMVC编程步骤

  3.1 导包 : spring-webmvc

    》需要配置运行环境为 Tomcat

  3.2 添加一个spring的配置文件

  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" xmlns:context="http://www.springframework.org/schema/context"
  4. xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee"
  5. xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
  6. xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util"
  7. xmlns:jpa="http://www.springframework.org/schema/data/jpa"
  8. xsi:schemaLocation="
  9. http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
  10. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
  11. http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
  12. http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
  13. http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
  14. http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
  15. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
  16. http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
  17. http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">
  18.  
  19. <!-- 配置HandlerMapping -->
  20. <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
  21. <property name="mappings">
  22. <props>
  23. <prop key="/hello.do">helloController</prop>
  24. </props>
  25. </property>
  26. </bean>
  27. <bean id="helloController" class="test.TestController"/>
  28.  
  29. <!-- 配置ViewResolver -->
  30. <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  31. <property name="prefix" value="/WEB-INF/"/>
  32. <property name="suffix" value=".jsp"/>
  33. </bean>
  34.  
  35. </beans>

spring配置文件模板

  3.3 配置DispatcherServlet

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
  3. <display-name>springmvc01s</display-name>
  4. <welcome-file-list>
  5. <welcome-file>index.html</welcome-file>
  6. <welcome-file>index.htm</welcome-file>
  7. <welcome-file>index.jsp</welcome-file>
  8. <welcome-file>default.html</welcome-file>
  9. <welcome-file>default.htm</welcome-file>
  10. <welcome-file>default.jsp</welcome-file>
  11. </welcome-file-list>
  12.  
  13. <servlet>
  14. <servlet-name>springmvc</servlet-name>
  15. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  16. <!--
  17. DispatcherServlet的初始化方法会启动spring容器,
  18. contextConfigLocation用于指定spring配置文件的位置
  19. -->
  20. <init-param>
  21. <param-name>contextConfigLocation</param-name>
  22. <param-value>classpath:spring-mvc.xml</param-value>
  23. </init-param>
  24. <load-on-startup>1</load-on-startup>
  25. </servlet>
  26. <servlet-mapping>
  27. <servlet-name>springmvc</servlet-name>
  28. <url-pattern>*.do</url-pattern>
  29. </servlet-mapping>
  30. </web-app>

DispatcherServlet配置文件

  3.4 写Controller(处理器)

  3.5 写jsp

  3.6 配置HandlerMapping,ViewResolver

4 SpringMVC编程步骤(利用注解实现) 

  step1. 导包。
  step2. 添加一个spring的配置文件。

  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" xmlns:context="http://www.springframework.org/schema/context"
  4. xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee"
  5. xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
  6. xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util"
  7. xmlns:jpa="http://www.springframework.org/schema/data/jpa"
  8. xsi:schemaLocation="
  9. http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
  10. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
  11. http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
  12. http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
  13. http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
  14. http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
  15. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
  16. http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
  17. http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">
  18. <!-- 配置注解扫描 -->
  19. <context:component-scan base-package="test"></context:component-scan>
  20. <!-- 配置mvc注解扫描 -->
  21. <mvc:annotation-driven></mvc:annotation-driven>
  22.  
  23. <!-- 配置ViewResolver -->
  24. <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  25. <property name="prefix" value="/WEB-INF/"/>
  26. <property name="suffix" value=".jsp"/>
  27. </bean>
  28.  
  29. </beans>

spring配置文件模板(利用注解实现)

  step3. 配置DispatcherServlet。
  step4. 写Controller(处理器)。
    要求:
      a. 不用实现Controller接口。
      b. 可以添加多个方法。
      c. 方法名不做要求,返回值可以是ModelAndView, 也可以是String。
      d. 将@Controller添加到类名前。
      e. 将@RequestMapping添加到类名前或者方法前。
  step5.写jsp。
  step6.配置ViewResolver,组件扫描,mvc注解扫描。
  注:只有配置了mvc注解扫描,@RequestMapping才起 作用

5 参数获取、响应数据

  待更新...2017年5月13日22:41:15

本博客源代码2:点击前往

本博客源代码1:点击前往

SpringMVC_01 SpringMVC五大组件、SpringMVC编程步骤(不使用注解进行配置)、SpringMVC编程步骤(利用注解进行配置)、参数获取、响应数据的更多相关文章

  1. SpringMVC 五大组件

    DispatcherServlet HandleMapping Controller ModeAndView ViewResolver 1.DispatcherServlet 这个控件是SpringM ...

  2. springboot springmvc拦截器 拦截POST、PUT、DELETE请求参数和响应数据,并记录操作日志

    1.操作日志实体类 @Document(collection = "operation_log") @Getter @Setter @ToString public class O ...

  3. Java框架之SpringMVC 03-RequestMapping-请求数据-响应数据

    SpringMVC SpringMVC是一种轻量级的.基于MVC的Web层应用框架. 通过一套 MVC 注解,让 POJO 成为处理请求的控制器,而无须实现任何接口. 采用了松散耦合可插拔组件结构,比 ...

  4. springMVC三大组件、spring主要jar包、

    一.springMVC三大组件 处理器映射器   RequestMappingHandlerMapping 处理器适配器 RequestMappingHandlerAdapter 视图解析器  Int ...

  5. SpringMVC架构&组件&执行流程

    SpringMVC架构: 组件: DIspatcherServlet:前端控制器.相当于mvc模式的c,是整个流程控制的中心,负责调用其他组件处理用户的请求,降低了组件之间的耦合性. HandlerM ...

  6. Spring Boot2.0之注解方式启动Springmvc

    回顾下springmvc原理图: DispatcherServlet是Spring MVC的核心,每当应用接受一个HTTP请求,由DispatcherServlet负责将请求分发给应用的其他组件. 在 ...

  7. Spring Boot自动配置SpringMVC(一)

    实际上在关于Spring Boot自动配置原理实战的文章Spring Boot自动配置实战 - 池塘里洗澡的鸭子 - 博客园 (cnblogs.com)中,可以看到我们使用到了@ReqeusMappi ...

  8. SpringMVC,Spring,Hibernate,Mybatis架构开发搭建之SpringMVC部分

    SpringMVC,Spring,Hibernate,Mybatis架构开发搭建之SpringMVC部分 辞职待业青年就是有很多时间来写博客,以前在传统行业技术强度相对不大,不处理大数据,也不弄高并发 ...

  9. 第7章—SpringMVC高级技术—不用web.xml,而使用java类配置SpringMVC

    不用web.xml,而使用java类配置SpringMVC DispatcherServlet是Spring MVC的核心,按照传统方式, 需要把它配置到web.xml中. 我个人比较不喜欢XML配置 ...

随机推荐

  1. c#线程中下载文件到本地

    额,太懒了 直接上示例代码... /// <summary> /// 下载文件到本地 2017-05-31 /// </summary> /// <param name= ...

  2. Ceph pg分裂流程及可行性分析

    转自:https://www.ustack.com/blog/ceph-pg-fenlie/ 1 pg分裂 Ceph作为一个scalable的分布式系统,集群规模会逐渐增大,为了保证数据分布的均匀性, ...

  3. N!的阶乘附带简单大整数类的输入输出(暂时没有深入的了解)

    Given an integer N(0 ≤ N ≤ 10000), your task is to calculate N! 我的思路:就想着大整数类去了,才发现自己还不能很好的掌握,其实这是一个大 ...

  4. swing之JDialog

    package canying; import java.awt.Color; import java.awt.Graphics; import java.awt.Image; import java ...

  5. redis设置为null问题

    查看源码后发现,redis没有删除方法,本想给他设置为null,但是redis报错,所有仔细想了一下,发现redis提供了一个时间限制方法,所有可以让redis的时间限制为1s,就想当于删除redis ...

  6. SpringCloud基础教程学习记录

    这个学习记录是学习自翟永超前辈的SpringCloud的基础教程. 自己写这个教程的目的主要是在于,想要更凝练总结一些其中的一些实用点,顺便做个汇总,这样自己在复习查看的时候更加方便,也能顺着自己的思 ...

  7. 多版本python管理miniconda(集成了virtualenv和pip功能)

    miniconda下载地址: https://conda.io/docs/user-guide/install/index.html Installing on Linux Download the ...

  8. 反射ORM 三层(for oracle)

    sql server and oracle 不同之处只在于: 1·参数@和: 2·自增和序列 3·oracle使用了存储过程以获得当前插入数据的ID DAL层的ORM using Oracle.Dat ...

  9. Oracle之into

    ), NVL() INTO SALE_ID, STORE_ID FROM SALEFROMSTORE WHERE ORDERID = IN_ORDER_ID; 这里要注意,into的时候是一个sele ...

  10. Cortex-M0(+)内核的处理器架构简介

    Cortex-M0(+)内核的处理器架构简介 2015年03月02日 16:51:12 阅读数:3158 系统架构 Cortex-M0处理器具有32位系统总线接口,以及32位地址线,即有4GB的地址空 ...