Spring Framework------>version4.3.5.RELAESE----->Reference Documentation学习心得----->Spring Framework中的spring web MVC模块

spring framework中的spring web MVC模块

1.概述

    • spring web mvc是spring框架中的一个模块
    • spring web mvc实现了web的MVC架构模式,可以被用于开发web网站
    • spring web mvc 实现web网站的原理,如下图:

2.使用spring web mvc开发web应用的步骤

  step1:在自己的工程中引入spring web mvc模块

  step2:配置spring web mvc模块 中的DispatcherServlet,告诉他要拦截哪些请求

  step3:

3.spring web mvc中相关知识点  

  3.1关于spring web mvc 中的DispatcherServlet

    • DispatcherServlet是spring web mvc 模块的核心部分,DispatcherServlet有如下功能

      • 接收用户请求,并将其分发给controller中的handling method
    • The DispatcherServlet is an actual Servlet (it inherits from the HttpServlet base class),
    • 要想DispatcherServlet 能够拦截到用户的请求,还需要做一些相应的配置,如使用URL mapping的方式将用户请求映射到DispatcherServlet。可以有多种方法来使得用户请求被映射到DispatcherServlet上,
      • 方法一,直接继承spring MVC 模块的WebApplicationInitializer接口,来配置spring MVC模块的DispatcherServlet,使其可以接收到用户请求

        •   MyWebApplicationInitializer.java
        • 将用户请求以URL方式映射到spring web mvc模块的 DispatcherServlet 上,从而使得用户请求能够通过DispatcherServlet被转交给controller来进行处理,并得到处理结果作为响应反馈给用户
        • 下面的例子中all requests starting with /example will be handled by the DispatcherServlet instance named example.
        • /*  1)WebApplicationInitializer is an interface provided by Spring MVC that ensures your code-based configuration is detected and automatically used to initialize any Servlet 3 container.    2)*/public class MyWebApplicationInitializer implements WebApplicationInitializer {
          
              @Override
              public void onStartup(ServletContext container) {
                  ServletRegistration.Dynamic registration = container.addServlet("example", new DispatcherServlet());
                  registration.setLoadOnStartup(1);
                  registration.addMapping("/example/*");
              }
          
          }

          使用上述方法(即Java代码的方法)配置URL映射,将用户请求交给DispatcherServlet来分发给对应的Controller,传统情况下使用web.xml文件配置相应映射的效果是一样的,如本例中上述代码的效果和下面的web.xml的配置代码是等价的(传统模式下使用web.xml配置用户请求URL,使得用户请求能够被Servlet拦截(如被spring web mvc的DispatcherServlet拦截))

        • 传统模式下在web.xml中配置请求URL和servlet的映射关系,如下所示:
        • <!--上面的Java代码和传统模式下web.xml文件下这一段代码是等效的  都是将用户请求/example/*交给web应用的servlet(例子中指的是spring web mvc中的DispatcherServlet)去处理   让servlet把接收到的用户请求交给controller层相应的handling method去处理--><web-app>
              <servlet>
                  <servlet-name>example</servlet-name>
                  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
                  <load-on-startup>1</load-on-startup>
              </servlet>
          
              <servlet-mapping>
                  <servlet-name>example</servlet-name>
                  <url-pattern>/example/*</url-pattern>
              </servlet-mapping>
          
          </web-app>
      • 方法二,继承 AbstractAnnotationConfigDispatcherServletInitializer (是方法一种所提及的WebApplicationInitializer接口的实现类)
时间: 2024-08-02 19:05:38

Spring Framework------>version4.3.5.RELAESE----->Reference Documentation学习心得----->Spring Framework中的spring web MVC模块的相关文章

step5---&gt;往工程中添加Spring框架的子模块SpringMVC----&gt;修改maven的配置文件pom.xml,向工程中添加spring框架的子模块springMVC

参考教程: 1)spring官网Building REST services with Spring     该网址处documentation内容: (不重要)首先介绍了什么是restful  web services,为什么spring中是实现了restful web services而不是实现SOAP web service (重要:Getting Started部分)介绍了

Spring Framework Reference Documentation 3.2.8.RELEASE 第23章中文翻译

23. JMS (Java Message Service) [中文翻译 by [email protected]] 23.1 介绍 Spring提供了一个JSM集成框架,简化了JMS API的使用.这点很像Spring对JDBC的集成. JMS大致提供生产消息和消费消息两类功能.JmsTemplate类用来生产消息和同步接收消息[译注:接收消息也就是消费消息].为了异步接收消息(异步接收消息类似于JavaEE的消息驱动Bean(Message-Driven Bean,MDB),Spring提供

Spring Framework 4.3.22.RELEASE Reference文档目录

<Spring Framework Reference Documentation 4.3.22.RELEASE> part I Spring Framework概述part II Spring Framework4.x的新特性part III 核心技术 7.IoC容器 8.Resources资源 9.校验.数据绑定.类型转换Validation, Data Binding, and Type Conversion 10.spring的EL表达式 11.spring AOP面向切面编程 12.

Springfox Reference Documentation

1. Introduction The Springfox suite of java libraries are all about automating the generation of machine and human readable specifications for JSON APIs written using the spring family of projects. Springfox works by examining an application, once, a

一些参考网站 - Reference Documentation - Website Address

Reference Documentation - Website Address MSDN Visual Studio 2015官方文档 https://msdn.microsoft.com/zh-CN/library/sc65sadd.aspx 提供visual stuio开发过程中的帮助文档 Microsoft Technet技术中心 https://technet.microsoft.com/zh-cn/ MySQL官方安装器 http://dev.mysql.com/downloads

(Spring文档翻译)Part V, the Web 17.1 Spring Web MVC framework介绍

指南文档的这个部分涵盖了Spring框架对表现层(特别是基于Web的表现层)以及WebSocket消息风格的web应用的支持. Spring框架拥有自己的web框架,Spring Web MVC,包含在前面几个章节.之后的几章是关于Spring框架对其他web技术的集成支持,像JSF等. 再之后是Spring框架的MVC porlet 框架. Spring 的MVC框架围绕着DispatcherServlet设计,DispatcherServlet将请求转发给handler,用可配置的handl

Spring 4 官方文档学习 Web MVC 框架

1.介绍Spring Web MVC 框架 Spring Web MVC 框架是围绕DispatcherServlet设计的,所谓DispatcherServlet就是将请求分发到handler,需要有配置好的handler映射.视图解析.本地化.时区.theme解决方案.还有上传文件的支持.默认的handler是基于@Controller和@RequestMapping注解.自Spring 3.0 起,@Controller注解还能用于RESTful,需要配合@PathVariable以及其他

SPring+Structs2实现的项目中进行Spring AOP时的相关小记

 SPring+Structs2实现的项目中进行Spring AOP时的相关小记 1.一般为了方便开发Structs2的项目中的action都会建立一个BaseAction如果继承了BaseAction中的子类进行AOP时,只能指定AOP中的PointCut为BaseAction 如果对应的BaseAction如果继承于ActionSupport的话,就只能定义AOP中的PointCut为ActionSupport了 因为Spring生成的代理类中,对同名的方法,只有一个,即子类重写父类的方

Features of Spring Web MVC

21.1.1 Features of Spring Web MVC Spring Web Flow Spring Web Flow (SWF) aims to be the best solution for the management of web application page flow. SWF integrates with existing frameworks like Spring MVC and JSF, in both Servlet and Portlet environ