Java Spring 中的 Dispatcher Servlet 是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2769467/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is Dispatcher Servlet in Spring?
提问by Kevin
In this image (which I got from here), HTTPrequest sends something to Dispatcher Servlet.
在这张图片(我从这里得到的)中,HTTP请求向Dispatcher Servlet发送了一些东西。
My Question is what does Dispatcher Servletdo?
我的问题是Dispatcher Servlet做什么?
Is it something like getting the information thrown from the web page and throwing it to the controller?
是否类似于从网页中获取信息并将其扔给控制器?
采纳答案by Affe
The job of the DispatcherServletis to take an incoming URI and find the right combination of handlers (generally methods on Controllerclasses) and views (generally JSPs) that combine to form the page or resource that's supposed to be found at that location.
DispatcherServlet的工作是获取传入的 URI 并找到处理程序(通常是控制器类上的方法)和视图(通常是 JSP)的正确组合,它们组合起来形成应该在该位置找到的页面或资源。
I might have
我可能有
- a file
/WEB-INF/jsp/pages/Home.jsp
and a methodon a class
@RequestMapping(value="/pages/Home.html") private ModelMap buildHome() { return somestuff; }
- 一份文件
/WEB-INF/jsp/pages/Home.jsp
和一个类的方法
@RequestMapping(value="/pages/Home.html") private ModelMap buildHome() { return somestuff; }
The Dispatcher servletis the bit that "knows" to call that method when a browser requests the page, and to combine its results with the matching JSP file to make an html document.
该调度的servlet是“知道”该位调用该方法,当浏览器请求的页面,其结果与匹配的JSP文件相结合,使HTML文档。
How it accomplishes this varies widely with configuration and Spring version.
它如何实现这一点因配置和 Spring 版本而异。
There's also no reason the end result has to be web pages. It can do the same thing to locate RMIend points, handle SOAPrequests, anything that can come into a servlet.
也没有理由最终结果必须是网页。它可以做同样的事情来定位RMI端点,处理SOAP请求,任何可以进入 servlet 的东西。
回答by skaffman
DispatcherServlet
is Spring MVC's implementation of the front controller pattern.
DispatcherServlet
是 Spring MVC 对前端控制器模式的实现。
See description in the Spring docs here.
请参阅此处的 Spring 文档中的说明。
Essentially, it's a servlet that takes the incoming request, and delegates processing of that request to one of a number of handlers, the mapping of which is specific in the DispatcherServlet
configuration.
本质上,它是一个 servlet,它接受传入的请求,并将该请求的处理委托给多个处理程序之一,其映射在DispatcherServlet
配置中是特定的。
回答by user2663609
We can say like DispatcherServlet
taking care of everything in Spring MVC.
我们可以说像DispatcherServlet
在 Spring MVC 中处理一切一样。
At web container start up:
在 Web 容器启动时:
DispatcherServlet
will be loaded and initialized by callinginit()
methodinit()
ofDispatcherServlet
will try to identify the Spring Configuration Document with naming conventions like"servlet_name-servlet.xml"
then all beans can be identified.
DispatcherServlet
将通过调用init()
方法加载和初始化init()
ofDispatcherServlet
将尝试使用命名约定来识别 Spring 配置文档,"servlet_name-servlet.xml"
然后可以识别所有 bean。
Example:
例子:
public class DispatcherServlet extends HttpServlet {
ApplicationContext ctx = null;
public void init(ServletConfig cfg){
// 1. try to get the spring configuration document with default naming conventions
String xml = "servlet_name" + "-servlet.xml";
//if it was found then creates the ApplicationContext object
ctx = new XmlWebApplicationContext(xml);
}
...
}
So, in generally DispatcherServlet
capture request URI and hand over to HandlerMapping
. HandlerMapping
search mapping bean with method of controller, where controller returning logical name(view). Then this logical name is send to DispatcherServlet
by HandlerMapping
. Then DispatcherServlet
tell ViewResolver
to give full location of view by appending prefix and suffix, then DispatcherServlet
give view to the client.
因此,通常DispatcherServlet
捕获请求 URI 并移交给HandlerMapping
. HandlerMapping
使用控制器方法搜索映射 bean,其中控制器返回逻辑名称(视图)。然后将此逻辑名称发送到DispatcherServlet
by HandlerMapping
。然后通过附加前缀和后缀DispatcherServlet
告诉ViewResolver
给出视图的完整位置,然后DispatcherServlet
将视图提供给客户端。
回答by user2663609
In Spring MVC, all incoming requests go through a single servlet. This servlet - DispatcherServlet
- is the front controller. Front controller is a typical design pattern in the web applications development. In this case, a single servlet receives all requests and transfers them to all other components of the application.
在 Spring MVC 中,所有传入请求都通过一个 servlet。这个servlet - DispatcherServlet
- 是前端控制器。前端控制器是 Web 应用程序开发中的一种典型设计模式。在这种情况下,单个 servlet 接收所有请求并将它们传输到应用程序的所有其他组件。
The task of the DispatcherServlet
is to send request to the specific Spring MVC controller.
的任务DispatcherServlet
是将请求发送到特定的 Spring MVC 控制器。
Usually we have a lot of controllers and DispatcherServlet
refers to one of the following mappers in order to determine the target controller:
通常我们有很多控制器,并DispatcherServlet
参考以下映射器之一来确定目标控制器:
BeanNameUrlHandlerMapping
;ControllerBeanNameHandlerMapping
;ControllerClassNameHandlerMapping
;DefaultAnnotationHandlerMapping
;SimpleUrlHandlerMapping
.
BeanNameUrlHandlerMapping
;ControllerBeanNameHandlerMapping
;ControllerClassNameHandlerMapping
;DefaultAnnotationHandlerMapping
;SimpleUrlHandlerMapping
.
If no configuration is performed, the DispatcherServlet
uses BeanNameUrlHandlerMapping
and DefaultAnnotationHandlerMapping
by default.
如果未执行任何配置,则默认DispatcherServlet
使用BeanNameUrlHandlerMapping
和DefaultAnnotationHandlerMapping
。
When the target controller is identified, the DispatcherServlet
sends request to it. The controller performs some work according to the request
(or delegate it to the other objects), and returns back to the DispatcherServlet
with the Model and the name of the View.
当目标控制器被识别时,DispatcherServlet
向它发送请求。控制器根据请求执行一些工作(或将其委托给其他对象),并返回DispatcherServlet
带有模型和视图名称的模型。
The name of the View is only a logical name. This logical name is then used to search for the actual View (to avoid coupling with the controller and specific View). Then DispatcherServlet
refers to the ViewResolver
and maps the logical name of the View to the specific implementation of the View.
视图的名称只是一个逻辑名称。然后使用这个逻辑名称来搜索实际的 View(以避免与控制器和特定的 View 耦合)。然后DispatcherServlet
引用ViewResolver
和映射View的逻辑名到View的具体实现。
Some possible Implementations of the ViewResolver
are:
的一些可能的实现ViewResolver
是:
BeanNameViewResolver
;ContentNegotiatingViewResolver
;FreeMarkerViewResolver
;InternalResourceViewResolver
;JasperReportsViewResolver
;ResourceBundleViewResolver
;TilesViewResolver
;UrlBasedViewResolver
;VelocityLayoutViewResolver
;VelocityViewResolver
;XmlViewResolver
;XsltViewResolver
.
BeanNameViewResolver
;ContentNegotiatingViewResolver
;FreeMarkerViewResolver
;InternalResourceViewResolver
;JasperReportsViewResolver
;ResourceBundleViewResolver
;TilesViewResolver
;UrlBasedViewResolver
;VelocityLayoutViewResolver
;VelocityViewResolver
;XmlViewResolver
;XsltViewResolver
.
When the DispatcherServlet
determines the view that will display the results it will be rendered as the response.
当DispatcherServlet
确定将显示结果的视图时,它将作为响应呈现。
Finally, the DispatcherServlet
returns the Response
object back to the client.
最后,DispatcherServlet
将Response
对象返回给客户端。
回答by Eduardo
I know this question is marked as solved already but I want to add a newer image explaining this pattern in detail(source: spring in action 4):
我知道这个问题已经被标记为已解决,但我想添加一个更新的图片来详细解释这个模式(来源:spring in action 4):
Explanation
解释
When the request leaves the browser (1), it carries information about what the user is asking for. At the least, the request will be carrying the requested URL. But it may also carry additional data, such as the information submitted in a form by the user.
当请求离开浏览器(1) 时,它携带有关用户请求的信息。至少,请求将携带所请求的 URL。但它也可能携带附加数据,例如用户以表格形式提交的信息。
The first stop in the request's travels is at Spring's DispatcherServlet. Like most Java- based web frameworks, Spring MVC funnels requests through a single front controller servlet. A front controller is a common web application pattern where a single servlet delegates responsibility for a request to other components of an application to per- form actual processing. In the case of Spring MVC, DispatcherServlet is the front controller. The DispatcherServlet's job is to send the request on to a Spring MVC controller. A controller is a Spring component that processes the request. But a typical application may have several controllers, and DispatcherServlet needs some help deciding which controller to send the request to. So the DispatcherServlet consults one or more handler mappings (2)to figure out where the request's next stop will be. The handler mapping pays particular attention to the URL carried by the request when making its decision. Once an appropriate controller has been chosen, DispatcherServlet sends the request on its merry way to the chosen controller (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 per- forms 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 suffi- cient—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). So that the controller doesn't get coupled to a particular view, the view name passed back to DispatcherServlet doesn't directly identify a specific JSP. It doesn't even necessarily suggest that the view is a JSP. Instead, it only carries a logical name that will be used to look up the actual view that will produce the result. The DispatcherServlet consults a view resolver (5)to map the logical view name to a spe- cific view implementation, which may or may not be a JSP. Now that DispatcherServlet knows which view will render the result, the request's job is almost over. Its final stop is at the view implementation (6), typically a JSP, where it delivers the model data. The request's job is finally done. The view will use the model data to render output that will be carried back to the client by the (not- so-hardworking) response object (7).
请求旅行的第一站是 Spring 的 DispatcherServlet。与大多数基于 Java 的 Web 框架一样,Spring MVC 通过单个前端控制器 servlet 收集请求。前端控制器是一种常见的 Web 应用程序模式,其中单个 servlet 将请求的责任委托给应用程序的其他组件以执行实际处理。在 Spring MVC 的情况下,DispatcherServlet 是前端控制器。DispatcherServlet 的工作是将请求发送到 Spring MVC 控制器。控制器是处理请求的 Spring 组件。但是一个典型的应用程序可能有多个控制器,并且 DispatcherServlet 需要一些帮助来决定将请求发送到哪个控制器。所以 DispatcherServlet 会咨询一个或多个处理程序映射(2)找出请求的下一站在哪里。处理程序映射在做出决定时特别注意请求携带的 URL。一旦选择了合适的控制器,DispatcherServlet 就会以快乐的方式将请求发送到所选控制器(3). 在控制器处,请求丢弃其有效负载(用户提交的信息)并耐心等待控制器处理该信息。(实际上,设计良好的控制器本身很少或不执行任何处理,而是将业务逻辑的责任委托给一个或多个服务对象。)控制器执行的逻辑通常会产生一些需要返回到用户并显示在浏览器中。此信息称为模型。但是将原始信息发送回用户是不够的——它需要以用户友好的格式进行格式化,通常是 HTML。为此,需要将信息提供给视图,通常是 JavaServer Page (JSP)。控制器做的最后一件事是打包模型数据并确定应该呈现输出的视图的名称。然后它将请求连同模型和视图名称一起发送回 DispatcherServlet(4). 为了使控制器不会与特定视图耦合,传递回 DispatcherServlet 的视图名称不会直接标识特定的 JSP。它甚至不一定表明该视图是一个 JSP。相反,它只携带一个逻辑名称,用于查找将产生结果的实际视图。DispatcherServlet 咨询视图解析器(5)以将逻辑视图名称映射到特定的视图实现,该实现可能是也可能不是 JSP。现在 DispatcherServlet 知道哪个视图将呈现结果,请求的工作就快结束了。它的最后一站是视图实现(6),通常是一个 JSP,它提供模型数据。请求的工作终于完成了。视图将使用模型数据来呈现输出,该输出将由(不那么努力的)响应对象(7)携带回客户端。
回答by anjali shrivas
Dispatcher Controller are displayed in the figure all the incoming request is in intercepted by the dispatcher servlet that works as front controller. The dispatcher servlet gets an entry to handler mapping from the XML file and forwords the request to the Controller.
图中显示了调度器控制器,所有传入的请求都被作为前端控制器的调度器 servlet 拦截。调度程序 servlet 从 XML 文件获取处理程序映射的条目,并将请求转发给控制器。
回答by kartik
<?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>