SpringMVC源码剖析(三)9大组件初识

我们在之前initStrategies时  完成了对9大组件的初始化

protected void initStrategies(ApplicationContext context) {
		initMultipartResolver(context);
		initLocaleResolver(context);
		initThemeResolver(context);
		initHandlerMappings(context);
		initHandlerAdapters(context);
		initHandlerExceptionResolvers(context);
		initRequestToViewNameTranslator(context);
		initViewResolvers(context);
		initFlashMapManager(context);
	}
这里不分先后顺序

1.HandleMapping

作用根据Request找到相应的Handler和Interceptors

这个接口只有一个方法

HandlerExecutionChain getHandler(PortletRequest request) throws Exception;

关于它的使用后面再讲

2.HandlerAdapter

它里面有3个方法

boolean supports(Object handler);
ModelAndView handle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception;
long getLastModified(HttpServletRequest request, Object handler);
supports判断是否可以使用某个Handler

handler方法是用来具体使用Handler干活的

getLastModified是获取资源的最后一次修改时间          handler方法是Object类型提供了更大的自由

3.HandlerExceptionResolver

字面理解:根据异常设置ModelAndView之后再交给render方法进行渲染

接口只有一个方法resolveException从异常中解析出ModelAndView   具体实现维护一个Map

4.ViewResolver

作用将String类型的视图名和Locale解析为View类型的视图只有一个方法

View resolveViewName(String viewName, Locale locale) throws Exception;
具体的后面再讲

5.RequestToViewNameTranslator

作用从request中获取viewName

public interface RequestToViewNameTranslator {

	/**
	 * Translate the given {@link HttpServletRequest} into a view name.
	 * @param request the incoming {@link HttpServletRequest} providing
	 * the context from which a view name is to be resolved
	 * @return the view name (or {@code null} if no default found)
	 * @throws Exception if view name translation fails
	 */
	String getViewName(HttpServletRequest request) throws Exception;

}

一个接口,该组件在SpringMVC容器中只可以配置一个,所有的request到viewName的转化规则在一个Translator里面全部完成。

6.LocaleResolver

我们知道解析视图需要两个参数第一个视图名  第二个Locale

作用从Request中解析出Locale  Locale就是zh-cn之类 代表一个区域  有了这个可以对不同区域的用户展示不同的结果 这也是i18n(国际化)的原理

public interface LocaleResolver {
	Locale resolveLocale(HttpServletRequest request);
	void setLocale(HttpServletRequest request, HttpServletResponse response, Locale locale);
}

这里大概了解下有这个东西就行了

7.ThemResolver

作用 解析主题

定义

public interface ThemeResolver {
	String resolveThemeName(HttpServletRequest request);
	void setThemeName(HttpServletRequest request, HttpServletResponse response, String themeName);
}

在SpringMVC中一套主题对应一个properties文件,里面存放了和当前主题相关的所有资源文件 比如图片 ,css样式表

具体的后面再讲

8.MultipartResolver

作用用于处理上传请求  处理方法将普通的request包装成MultipartHttpServletRequest,后者可以直接用getFile得到File

public interface MultipartResolver {
	boolean isMultipart(HttpServletRequest request);
	MultipartHttpServletRequest resolveMultipart(HttpServletRequest request) throws MultipartException;
	void cleanupMultipart(MultipartHttpServletRequest request);
}
3个方法分别是判断是不是上传请求,将request包装成MultipartHttpServletRequest,处理完之后清理上传中产生的临时资源

9.FlashMapManger

作用主要来管理FlashMap的

public interface FlashMapManager {
	FlashMap retrieveAndUpdate(HttpServletRequest request, HttpServletResponse response);
	void saveOutputFlashMap(FlashMap flashMap, HttpServletRequest request, HttpServletResponse response);
}
retrieveAndUpdate用于恢复参数,并将恢复过的和超时的参数从保存介质中删除

saveOutFlashMap将参数保存起来

默认实现SessionFlashMapManger

后面再细讲

而在SpringMVC中每个组件的实现原理还值得我们去慢慢学习

后面再更新


欢迎关注我的个人订阅号,我会推送更好的文章给大家

订阅号
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值