在这张图中(我从这里得到的),HTTP请求向Dispatcher Servlet发送一些东西。

我的问题是Dispatcher Servlet做什么?

它是否类似于从网页中获得抛出的信息并将其抛出给控制器?


当前回答

DispatcherServlet的工作是获取一个传入URI并找到处理程序(通常是Controller类上的方法)和视图(通常是jsp)的正确组合,它们组合在一起形成应该在该位置找到的页面或资源。

我可能有

文件/WEB-INF/jsp/pages/Home.jsp 和类上的方法 @RequestMapping (value = " /页面/ home。") 私有ModelMap buildHome() { 返回somestuff; }

Dispatcher servlet“知道”在浏览器请求页面时调用该方法,并将其结果与匹配的JSP文件结合起来以生成html文档。

它如何实现这一点,随着配置和Spring版本的不同而有很大差异。

最终的结果也没有理由非得是网页。它可以做同样的事情来定位RMI端点、处理SOAP请求以及任何可以进入servlet的事情。

其他回答

DispatcherServlet是Spring MVC前端控制器模式的实现。

请在这里查看Spring文档中的描述。

本质上,它是一个接受传入请求的servlet,并将该请求的处理委托给许多处理程序中的一个,这些处理程序的映射在DispatcherServlet配置中是特定的。

Dispatcher Controller显示在图中,所有传入的请求都被作为前端控制器的Dispatcher servlet拦截。 dispatcher servlet从XML文件获取到处理程序映射的条目,并将请求转发给Controller。

DispatcherServlet的工作是获取一个传入URI并找到处理程序(通常是Controller类上的方法)和视图(通常是jsp)的正确组合,它们组合在一起形成应该在该位置找到的页面或资源。

我可能有

文件/WEB-INF/jsp/pages/Home.jsp 和类上的方法 @RequestMapping (value = " /页面/ home。") 私有ModelMap buildHome() { 返回somestuff; }

Dispatcher servlet“知道”在浏览器请求页面时调用该方法,并将其结果与匹配的JSP文件结合起来以生成html文档。

它如何实现这一点,随着配置和Spring版本的不同而有很大差异。

最终的结果也没有理由非得是网页。它可以做同样的事情来定位RMI端点、处理SOAP请求以及任何可以进入servlet的事情。

<?xml version='1.0' encoding='UTF-8' ?>
<!-- was: <?xml version="1.0" encoding="UTF-8"?> -->
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
               http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">

    <bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>
    <context:component-scan base-package="com.demo" />
    <context:annotation-config />

    <mvc:annotation-driven />


    <bean id="viewResolver"
          class="org.springframework.web.servlet.view.InternalResourceViewResolver"
          p:prefix="/WEB-INF/jsp/"
          p:suffix=".jsp" />

    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="datasource" />
    </bean> 

          <bean id="datasource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />  
        <property name="url" value="jdbc:mysql://localhost:3306/employee" />
        <property name="username" value="username" />
        <property name="password" value="password" />
    </bean> 

</beans>

我知道这个问题已经被标记为已经解决,但我想添加一个更新的图像来详细解释这个模式(来源:spring in action 4):

解释

当请求离开浏览器(1)时,它携带了关于用户请求的信息。至少,请求将携带所请求的URL。但它也可能携带额外的数据,例如用户在表单中提交的信息。

请求传递的第一站是Spring的DispatcherServlet。像大多数基于Java的web框架一样,Spring MVC通过一个前端控制器servlet来引导请求。前端控制器是一种常见的web应用程序模式,其中单个servlet将请求的责任委托给应用程序的其他组件来执行实际处理。在Spring MVC中,DispatcherServlet是前端控制器。

DispatcherServlet的任务是将请求发送到Spring MVC控制器。控制器是处理请求的Spring组件。但是一个典型的应用程序可能有几个控制器,DispatcherServlet需要一些帮助来决定将请求发送到哪个控制器。

因此DispatcherServlet咨询一个或多个处理程序映射(2)来确定请求的下一站将在哪里。处理程序映射在做出决定时特别注意请求携带的URL。 一旦选择了合适的控制器,DispatcherServlet就会将请求发送到所选择的控制器(3)。

At the controller, the request drops off its payload (the information submitted by the user) and patiently waits while the controller processes that information. (Actually, a well-designed controller performs little or no processing itself and instead delegates responsibility for the business logic to one or more service objects.) The logic performed by a controller often results in some information that needs to be carried back to the user and displayed in the browser. This information is referred to as the model. But sending raw information back to the user isn’t sufficient. It needs to be formatted in a user-friendly format, typically HTML. For that, the information needs to be given to a view, typically a JavaServer Page (JSP). One of the last things a controller does is package up the model data and identify the name of a view that should render the output. It then sends the request, along with the model and view name, back to the DispatcherServlet (4).

因此控制器不会耦合到特定的视图,因此传递回DispatcherServlet的视图名不会直接标识特定的JSP。它甚至不一定表明视图是JSP。相反,它只携带一个逻辑名称,用于查找将产生结果的实际视图。DispatcherServlet咨询视图解析器(5),将逻辑视图名映射到特定的视图实现,该视图实现可能是也可能不是JSP。

既然DispatcherServlet知道哪个视图将呈现结果,请求的工作就差不多结束了。它的最后一站是视图实现(6),通常是JSP,它在这里交付模型数据。请求的工作终于完成了。视图将使用模型数据来呈现输出,这些输出将由(不那么努力的)响应对象(7)带回客户端。