组件概览
HandlerMapping
根据 request 找到对应的处理器 Handler 和 Interceptors。内部只有一个方法
HandlerExecutionChain getHandler(HttpServletRequest request) throws Exception;
HandlerAdapter
Handler 适配器,内部方法如下:
boolean supports(Object handler);//判断是否可以使用某个 Handler
ModelAndView handle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception; //具体使用
long getLastModified(HttpServletRequest request, Object handler);//获取资源上一次修改的时间
HandlerExceptionResolver
根据异常设置 ModelAndView ,再交给 render 方法进行渲染。
ModelAndView resolveException(
HttpServletRequest request, HttpServletResponse response, @Nullable Object handler, Exception ex)
ViewResolver
用来将 String 类型的视图名和 Locale 解析为 View 类型的视图。
View resolveViewName(String viewName, Locale locale) throws Exception;
它的一个实现类 BeanNameViewResolver,它重写 resolveViewName 方法如下:
public View resolveViewName(String viewName, Locale locale) throws BeansException {
ApplicationContext context = getApplicationContext();
//如果应用上下文没有找到视图,返回 null
if (!context.containsBean(viewName)) {
if (logger.isDebugEnabled()) {
logger.debug("No matching bean found for view name '" + viewName + "'");
}
// Allow for ViewResolver chaining...
return null;
}
//如果找到的视图类型不匹配,也返回 null
if (!context.isTypeMatch(viewName, View.class)) {
if (logger.isDebugEnabled()) {
logger.debug("Found matching bean for view name '" + viewName +
"' - to be ignored since it does not implement View");
}
// Since we're looking into the general ApplicationContext here,
// let's accept this as a non-match