天天看點

17.2 The DispatcherServlet

綜述:

  Spring’s web MVC framework is, like many other web MVC frameworks, request-driven, designed around a central Servlet that dispatches requests to controllers and offers other functionality that facilitates the development of web applications. Spring’s DispatcherServlet however, does more than just that. It is completely integrated with the Spring IoC container and as such allows you to use every other feature that Spring has.

  The request processing workflow of the Spring Web MVC DispatcherServlet is illustrated in the following diagram. The pattern-savvy reader will recognize that the DispatcherServlet is an expression of the "Front Controller" design pattern (this is a pattern that Spring Web MVC shares with many other leading web frameworks).      

  spring的web mvc架構,像其他的web mvc架構一樣,請求驅動,圍繞着一個中央servlet設計的,該中央servlet分發請求到控制器,并提供其他功能以促進web應用的發展。然而,spring的dispatcherServlet,做的不隻這麼多。它完全使用了spring ioc容器進行內建,正因如此才允許你使用spring擁有的所有特性

  spring mvc dispatcherservlet的請求處理流程,列舉在下面的圖表裡。pattren-savvy的讀者将會識别出DispatcherServlet前置控制器的表達模式(這是一個模式,那些spring web mvc與很多其他領先的web架構共享的)。

17.2 The DispatcherServlet
The request processing workflow in Spring Web MVC (high level)

The DispatcherServlet is an actual Servlet (it inherits from the HttpServlet base class), and as such is declared in the web.xml of your web application. You need to map requests that you want the DispatcherServlet to handle, by using a URL mapping in the same web.xml file. This is standard Java EE Servlet configuration; the following example shows such a DispatcherServlet declaration and mapping:      

spring web mvc的請求處理流程:

  DispatcherServlet實際上就是一個servlet,它繼承自HttpServlet這個基礎類,并且可以在你的web項目的web.xml中聲明。你隻需要将該控制器處理的請求分發給它即可,在同一個web.xml檔案中,通過使用一個url映射。這是一個标準的java ee servlet配置,下面的例子說明了該servlet的聲明和映射:

<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>      
In the preceding example, all requests starting with /example will be handled by the DispatcherServlet instance named example. In a Servlet 3.0+ environment, you also have the option of configuring the Servlet container programmatically. Below is the code based equivalent of the above web.xml example:      

  上面的例子,所有以/example開頭的請求都将會被名為example的DispatcherServlet處理。在servlet 3.0以上的環境中,你也可以通過程式設計方式代替配置該servlet的手段。下面的代碼就是同上面web.xml示例相對應的代碼。

public class MyWebApplicationInitializer implements WebApplicationInitializer {

    @Override
    public void onStartup(ServletContext container) {
        ServletRegistration.Dynamic registration = container.addServlet("dispatcher", new DispatcherServlet());
        registration.setLoadOnStartup(1);
        registration.addMapping("/example/*");
    }

}      
  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. An abstract base class implementation of this interface named AbstractDispatcherServletInitializer makes it even easier to register the DispatcherServlet by simply specifying its servlet mapping. See Code-based Servlet container initialization for more details.

  The above is only the first step in setting up Spring Web MVC. You now need to configure the various beans used by the Spring Web MVC framework (over and above the DispatcherServlet itself).

  As detailed in Section 5.15, “Additional Capabilities of the ApplicationContext”, ApplicationContext instances in Spring can be scoped. In the Web MVC framework, each DispatcherServlet has its own WebApplicationContext, which inherits all the beans already defined in the root WebApplicationContext. These inherited beans can be overridden in the servlet-specific scope, and you can define new scope-specific beans local to a given Servlet instance.      
  WebApplicationInitializer spring mvc提供的一個接口,確定你在編碼中的配置能夠被發現,并自動被使用以用來初始化servlet3容器。一個抽象的基礎類實作該接口,被命名為
AbstractDispatcherServletInitializer使得它更容易去注冊該DispatcherServlet通過簡單的指定它的servlet映射。看Code-based Servlet container initialization有更多詳細資訊。
  上面的内容僅僅是設定spring-mvc的第一步,現在,你需要配置spring mvc framework使用的各種beans()。
  就像在5.15中描述的一樣,應用上下文添加的能力,spring的應用上下文執行個體是能夠被審查的。在web mvc 架構中,每個DispatcherServlet都有它自己的webApplicationContext,該~繼承了在基礎WebApplicationContext中定義的所有beans。這些繼承的beans能夠在servlet-specific scope中重寫,你能夠定義新的scope-specific beans 給servlet執行個體。

Figure 17.2. Context hierarchy in Spring Web MVC      
Upon initialization of a DispatcherServlet, Spring MVC looks for a file named [servlet-name]-servlet.xml in the WEB-INF directory of your web application and creates the beans defined there, overriding the definitions of any beans defined with the same name in the global scope.      

  在初始化DispatcherServlet之前,spring mvc在你的應用程式的WEB-INF目錄中查找一個檔案名servlet-name-servlet.xml,并根據定義的内容建立相應的beans,覆寫全局中同樣名稱的任何beans。

Consider the following DispatcherServlet Servlet configuration (in the web.xml file):      

注意下面的DispatcherServlet配置

<web-app>
    <servlet>
        <servlet-name>golfing</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>golfing</servlet-name>
        <url-pattern>/golfing/*</url-pattern>
    </servlet-mapping>
</web-app      
With the above Servlet configuration in place, you will need to have a file called /WEB-INF/golfing-servlet.xml in your application; this file will contain all of your Spring Web MVC-specific components (beans). You can change the exact location of this configuration file through a Servlet initialization parameter (see below for details).      

用上面的servlet配置替換,你需要一個叫做/WEB-INF/golfing-servlet.xml的檔案在你的系統中,這個檔案将會包含所有你spring mvc 特定的元件。你可以通過servlet的初始化參數配置,改變這個檔案存放的地方。如下:

<web-app>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/root-context.xml</param-value>
    </context-param>
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value></param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
</web-app>      
The WebApplicationContext is an extension of the plain ApplicationContext that has some extra features necessary for web applications. It differs from a normal ApplicationContext in that it is capable of resolving themes (see Section 17.9, “Using themes”), and that it knows which Servlet it is associated with (by having a link to the ServletContext). The WebApplicationContext is bound in the ServletContext, and by using static methods on the RequestContextUtils class you can always look up the WebApplicationContext if you need access to it.      

WebApplicationContext是簡單ApplicationContext的一個擴充,有一些額外的特性是應用程式所需要的。與一個普通的ApplicationContext不同,在WebApplicationContext中包含了解析主題。Web被綁定在ServletContext中,通過使用RequestContextUtils類的靜态方法,當你需要使用它時,你總能找到它。

轉載于:https://www.cnblogs.com/brolanda/p/4534733.html