import java.util.List;
import java.util.Locale;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.HandlerAdapter;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.HandlerExecutionChain;
import org.springframework.web.servlet.HandlerMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.View;
import org.springframework.web.servlet.ViewResolver;
public class DispatcherServletDemo {
private List<HandlerMapping> handlerMappings = null;
private List<HandlerAdapter> handlerAdapters = null;
private List<HandlerExceptionResolver> handlerExceptionResolvers = null;
private List<ViewResolver> viewResolvers = null;
public void doService(HttpServletRequest request, HttpServletResponse response) throws Exception {
ModelAndView modelAndView = null;
Exception dispatchException = null;
HandlerExecutionChain handlerChain = null;
try {
// get handlerChain that consisting of "handler object" and any "handler interceptors"
handlerChain = getHandler(request);
if (handlerChain == null) {
response.sendError(HttpServletResponse.SC_NOT_FOUND);
return;
}
// get handlerAdapter
HandlerAdapter handlerAdapter = getHandlerAdapter(handlerChain);
// insert "handler interceptors" preHandle method
// call "handler object" by handlerAdapter
modelAndView = handlerAdapter.handle(request, response, handlerChain.getHandler());
// insert "handler interceptors" postHandle method
} catch (Exception ex) {
dispatchException = ex;
}
if (dispatchException != null) {
modelAndView = processHandlerException(request, response, handlerChain.getHandler(), dispatchException);
}
if (modelAndView != null) {
render(modelAndView, request, response);
}
// insert "handler interceptors" afterCompletion method
}
private HandlerExecutionChain getHandler(HttpServletRequest request) throws Exception {
for (HandlerMapping handlerMapping : handlerMappings) {
HandlerExecutionChain handler = handlerMapping.getHandler(request);
if (handler != null) {
return handler;
}
}
return null;
}
private HandlerAdapter getHandlerAdapter(HandlerExecutionChain handlerChain) throws ServletException {
for (HandlerAdapter handlerAdapter : handlerAdapters) {
if (handlerAdapter.supports(handlerChain)) {
return handlerAdapter;
}
}
throw new ServletException("No adapter for handler");
}
private ModelAndView processHandlerException(HttpServletRequest request, HttpServletResponse response,
Object handler, Exception dispatchException) throws Exception {
ModelAndView exModelAndView = null;
for (HandlerExceptionResolver handlerExceptionResolver : handlerExceptionResolvers) {
exModelAndView = handlerExceptionResolver.resolveException(request, response, handler, dispatchException);
if (exModelAndView != null) {
break;
}
}
if (exModelAndView != null) {
return exModelAndView;
}
throw dispatchException;
}
private void render(ModelAndView modelAndView, HttpServletRequest request, HttpServletResponse response)
throws Exception {
Locale locale = null; // LocaleResolver
View view = null;
if (modelAndView.isReference()) {
for (ViewResolver viewResolver : this.viewResolvers) {
View v = viewResolver.resolveViewName(modelAndView.getViewName(), locale);
if (v != null) {
view = v;
break;
}
}
if (view == null) {
throw new ServletException("Could not resolve view with the name");
}
} else {
view = modelAndView.getView();
}
view.render(modelAndView.getModel(), request, response);
}
}
相关推荐
在 Spring MVC 中, jsp 文件中尽量不要有 Java 代码, 只有 HTML 代码和"迭代(forEach)"与"判断(if)"两个jstl标签. jsp 文件只作为渲染(或称为视图 View)模板使用. 好了, 我们开始吧. 首先我们需要一个放在 WEB-INF...
主要给大家介绍了关于Spring MVC学习教程之DispatcherServlet请求处理的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
9.5.1. 理解Spring的声明式事务管理实现 9.5.2. 第一个例子 9.5.3. 回滚 9.5.4. 为不同的bean配置不同的事务语义 9.5.5. <tx:advice/> 有关的设置 9.5.6. 使用 @Transactional 9.5.6.1. @Transactional 有关的设置 ...
9.5.1. 理解Spring的声明式事务管理实现 9.5.2. 第一个例子 9.5.3. 回滚 9.5.4. 为不同的bean配置不同的事务语义 9.5.5. <tx:advice/> 有关的设置 9.5.6. 使用 @Transactional 9.5.7. 插入事务操作 9.5.8. ...
2.5.1. Spring MVC合理的默认值 2.5.2. Portlet 框架 2.5.3. 基于Annotation的控制器 2.5.4. Spring MVC的表单标签库 2.5.5. 对Tiles 2 支持 2.5.6. 对JSF 1.2支持 2.5.7. JAX-WS支持 2.6. 其他 2.6.1. 动态...
9.5.1. 理解Spring的声明式事务管理实现 9.5.2. 第一个例子 9.5.3. 回滚 9.5.4. 为不同的bean配置不同的事务语义 9.5.5. <tx:advice/> 有关的设置 9.5.6. 使用 @Transactional 9.5.7. 插入事务操作 9.5.8. ...
2.5.1. Spring MVC合理的默认值 2.5.2. Portlet 框架 2.5.3. 基于Annotation的控制器 2.5.4. Spring MVC的表单标签库 2.5.5. 对Tiles 2 支持 2.5.6. 对JSF 1.2支持 2.5.7. JAX-WS支持 2.6. 其他 2.6.1. 动态...
关闭Spring MVC DispatcherServlet vii. 65.7. 关闭默认的MVC配置 Spring Boot参考指南 7 viii. 65.8. 自定义ViewResolvers v. 66. 日志 i. 66.1. 配置Logback ii. 66.2. 配置Log4j i. 66.2.1. 使用YAML或JSON配置...
mybatis实战教程mybatis in action之六与Spring MVC 的集成 mybatis实战教程mybatis in action之七实现mybatis分页源码下载 mybatis实战教程mybatis in action之八mybatis 动态sql语句 mybatis实战教程mybatis in ...
10.3.4 建立Spring的配置文档dispatcherServlet-servlet.xml 10.3.5 配置web.xml 10.3.6 启动Tomcat运行程序 10.4 Spring 的视图(View) 10.4.1 视图简介 10.4.2 视图解析 10.5 Spring的控制器(Controller) ...
10.3.4 建立Spring的配置文档dispatcherServlet-servlet.xml 10.3.5 配置web.xml 10.3.6 启动Tomcat运行程序 10.4 Spring 的视图(View) 10.4.1 视图简介 10.4.2 视图解析 10.5 Spring的控制器(Controller) ...
10.3.4 建立Spring的配置文档dispatcherServlet-servlet.xml 10.3.5 配置web.xml 10.3.6 启动Tomcat运行程序 10.4 Spring 的视图(View) 10.4.1 视图简介 10.4.2 视图解析 10.5 Spring的控制器(Controller) ...
1.spring mvc请所有的请求都提交给DispatcherServlet,它会委托应用系统的其他模块负责负责对请求进行真正的处理工作。 2.DispatcherServlet查询一个或多个HandlerMapping,找到处理请求的Controller. 3....
doodle是一个简易的Java MVC框架,它提供了类似于spring 的Bean容器、IOC、AOP、MVC等功能 代码简洁、轻量,适合用于参考学习spring 一行代码即可启动服务,内置Tomcat容器 DispatcherServlet请求逻辑处理采用责任链...
10.3.4 建立Spring的配置文档dispatcherServlet-servlet.xml 10.3.5 配置web.xml 10.3.6 启动Tomcat运行程序 10.4 Spring 的视图(View) 10.4.1 视图简介 10.4.2 视图解析 10.5 Spring的控制器(Controller) ...