在这张图中(我从这里得到的),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.
其他回答
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.
我们可以说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将视图传递给客户端。
DispatcherServlet的工作是获取一个传入URI并找到处理程序(通常是Controller类上的方法)和视图(通常是jsp)的正确组合,它们组合在一起形成应该在该位置找到的页面或资源。
我可能有
文件/WEB-INF/jsp/pages/Home.jsp 和类上的方法 @RequestMapping (value = " /页面/ home。") 私有ModelMap buildHome() { 返回somestuff; }
Dispatcher servlet“知道”在浏览器请求页面时调用该方法,并将其结果与匹配的JSP文件结合起来以生成html文档。
它如何实现这一点,随着配置和Spring版本的不同而有很大差异。
最终的结果也没有理由非得是网页。它可以做同样的事情来定位RMI端点、处理SOAP请求以及任何可以进入servlet的事情。
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>