Spring Framework------>version4.3.5.RELAESE----->Reference Documentation學習心得----->Spring Framework中的spring web MVC模組

來源:互聯網
上載者:User

標籤:etl   ini   xml檔案   tco   png   知識點   eth   initial   text   

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模組

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.