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

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接口的实现类)

 

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