在这张图中(我从这里得到的),HTTP请求向Dispatcher Servlet发送一些东西。
我的问题是Dispatcher Servlet做什么?
它是否类似于从网页中获得抛出的信息并将其抛出给控制器?
在这张图中(我从这里得到的),HTTP请求向Dispatcher Servlet发送一些东西。
我的问题是Dispatcher Servlet做什么?
它是否类似于从网页中获得抛出的信息并将其抛出给控制器?
当前回答
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.
其他回答
<?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>
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对象返回给客户机。