在这张图中(我从这里得到的),HTTP请求向Dispatcher Servlet发送一些东西。
我的问题是Dispatcher Servlet做什么?
它是否类似于从网页中获得抛出的信息并将其抛出给控制器?
在这张图中(我从这里得到的),HTTP请求向Dispatcher Servlet发送一些东西。
我的问题是Dispatcher Servlet做什么?
它是否类似于从网页中获得抛出的信息并将其抛出给控制器?
DispatcherServlet是Spring MVC前端控制器模式的实现。
请在这里查看Spring文档中的描述。
本质上,它是一个接受传入请求的servlet,并将该请求的处理委托给许多处理程序中的一个,这些处理程序的映射在DispatcherServlet配置中是特定的。
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中的一切。
在web容器启动时:
DispatcherServlet将通过调用来加载和初始化 init()方法 DispatcherServlet的init()将尝试识别Spring 具有命名约定的配置文档 "servlet_name-servlet.xml"则可以标识所有bean。
例子:
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);
}
...
}
因此,通常DispatcherServlet捕获请求URI并将其传递给HandlerMapping。HandlerMapping使用控制器的方法搜索映射bean,其中控制器返回逻辑名(视图)。然后这个逻辑名通过HandlerMapping发送给DispatcherServlet。然后DispatcherServlet通过附加前缀和后缀告诉ViewResolver给出视图的完整位置,然后DispatcherServlet将视图传递给客户端。
在Spring MVC中,所有传入的请求都通过单个servlet。这个servlet——DispatcherServlet——是前端控制器。前端控制器是web应用程序开发中的一种典型设计模式。在这种情况下,单个servlet接收所有请求并将它们传输到应用程序的所有其他组件。
DispatcherServlet的任务是向特定的Spring MVC控制器发送请求。
通常我们有很多控制器,DispatcherServlet引用以下映射器之一来确定目标控制器:
BeanNameUrlHandlerMapping; ControllerBeanNameHandlerMapping; ControllerClassNameHandlerMapping; DefaultAnnotationHandlerMapping; SimpleUrlHandlerMapping。
如果没有配置,DispatcherServlet默认使用BeanNameUrlHandlerMapping和DefaultAnnotationHandlerMapping。
确定目标控制器后,DispatcherServlet向其发送请求。控制器根据请求执行一些工作 (或将其委托给其他对象),并返回给DispatcherServlet,其中包含模型和视图的名称。
视图的名称只是一个逻辑名称。然后使用这个逻辑名来搜索实际的View(以避免与控制器和特定的View耦合)。然后DispatcherServlet引用ViewResolver并将视图的逻辑名映射到视图的特定实现。
ViewResolver的一些可能实现有:
BeanNameViewResolver; ContentNegotiatingViewResolver; FreeMarkerViewResolver; InternalResourceViewResolver; JasperReportsViewResolver; ResourceBundleViewResolver; TilesViewResolver; UrlBasedViewResolver; VelocityLayoutViewResolver; VelocityViewResolver; XmlViewResolver; XsltViewResolver。
当DispatcherServlet确定将显示结果的视图时,它将作为响应呈现。
最后,DispatcherServlet将Response对象返回给客户机。
我知道这个问题已经被标记为已经解决,但我想添加一个更新的图像来详细解释这个模式(来源: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)带回客户端。
Dispatcher Controller显示在图中,所有传入的请求都被作为前端控制器的Dispatcher servlet拦截。 dispatcher servlet从XML文件获取到处理程序映射的条目,并将请求转发给Controller。
<?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>
You can say Dispatcher Servlet acts as an entry and exit point for any request. Whenever a request comes it first goes to the Dispatcher Servlet(DS) where the DS then tries to identify its handler method ( the methods you define in the controller to handle the requests ), once the handler mapper (The DS asks the handler mapper) returns the controller the dispatcher servlet knows the controller which can handle this request and can now go to this controller to further complete the processing of the request. Now the controller can respond with an appropriate response and then the DS goes to the view resolver to identify where the view is located and once the view resolver tells the DS it then grabs that view and returns it back to you as the final response. I am adding an image which I took from YouTube from the channel Java Guides.