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);
}
}
相关推荐
功能说明: 环境说明: 开发软件:VS 2017 (版本2017以上即可,不能低于2017) 数据库:SqlServer2008r2(数据库版本无限制,都可以导入) 开发模式:mvc。。。
labview程序代码参考学习使用,希望对你有所帮助。
大米外贸商城系统 简称damishop 完全开源版,只需做一种语言一键开启全球133中语言自动翻译功能,价格实现自动汇率转换,集成微信支付宝 paypal以及国外主流支付方式,自带文章博客系统。 软件架构 基于MVC+语言包模式,增加控制台,API导入产品方便对接其他系统(带json示例数据)。 使用要求 PHP7.4+ MYSQL5.6+ REDIS(可选) 安装方法 composer install 打开安装向导安装 http://您的域名/install 特色 1、缓存层增加时间与批量like删除 2、API产品导入方便对接其他系统 3、增加控制台命令行,命令行生成语言翻译包 4、后台一键开启自动翻译模式,支持全球133中语言,由于google代理翻译需要收费,这个功能需要付费。 5、可选购物车与ajax修改购物车产品 6、一键结算checkout 7、增加网站前台自定义路由 方便seo 更新日志 v3.9.7 集成鱼码支付接口,方便个人站长即使收款到账使用 v3.9.3 更新内容 1:增加ueditor与旧编辑器切换 2:增加可视化布局插
labview程序代码参考学习使用,希望对你有所帮助。
labview程序代码参考学习使用,希望对你有所帮助。
毕设和企业适用springboot人工智能客服系统类及旅游规划平台源码+论文+视频