数据传递--------博客-----------springMVC源码分析--RequestToViewNameTranslator请求到视图名称的转换
参考来源:http://blog.csdn.net/qq924862077/article/details/54286976?utm_source=gold_browser_extension
RequestToViewNameTranslator可以在处理器返回的View为空时使用它根据Request获取viewName。RequestToViewNameTranslator提供的实现类只有一个DefaultRequestToViewNameTranslator。
接口RequestToViewNameTranslator中定义的如下:提供了getViewName抽象方法,其实就是根据request请求获取来组装视图名称。
- 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;
- }
其实现类DefaultRequestToViewNameTranslator中的实现如下:其实其简单实现就是将请求名称作为视图名称返回,逻辑还是比较简单的。
- @Override
- public String getViewName(HttpServletRequest request) {
- String lookupPath = this.urlPathHelper.getLookupPathForRequest(request);
- return (this.prefix + transformPath(lookupPath) + this.suffix);
- }
接下来我们看看RequestToViewNameTranslator在springMVC中的具体运行流程:
首先在DispatcherServlet的doDispatch函数中会设置默认的视图名
- protected void doDispatch(HttpServletRequest request, HttpServletResponse response) throws Exception {
- ......
- //设置默认的视图名称
- applyDefaultViewName(processedRequest, mv);
- ......
- }
在applyDefaultViewName中会判断ModelAndView的hasView为空时,就设置viewName
- private void applyDefaultViewName(HttpServletRequest request, ModelAndView mv) throws Exception {
- if (mv != null && !mv.hasView()) {
- mv.setViewName(getDefaultViewName(request));
- }
- }
getDefaultViewName的实现逻辑还是在ViewNameTranslator中。
- protected String getDefaultViewName(HttpServletRequest request) throws Exception {
- return this.viewNameTranslator.getViewName(request);
- }
在DefaultViewNameTranslator中实现的getViewName的逻辑如下,其实就是将请求路径作为ViewName
- @Override
- public String getViewName(HttpServletRequest request) {
- String lookupPath = this.urlPathHelper.getLookupPathForRequest(request);
- return (this.prefix + transformPath(lookupPath) + this.suffix);
- }
实现类DefaultViewNameTranslator的完整源码如下:
- public class DefaultRequestToViewNameTranslator implements RequestToViewNameTranslator {
- private static final String SLASH = "/";
- private String prefix = "";
- private String suffix = "";
- private String separator = SLASH;
- private boolean stripLeadingSlash = true;
- private boolean stripTrailingSlash = true;
- private boolean stripExtension = true;
- private UrlPathHelper urlPathHelper = new UrlPathHelper();
- public void setPrefix(String prefix) {
- this.prefix = (prefix != null ? prefix : "");
- }
- public void setSuffix(String suffix) {
- this.suffix = (suffix != null ? suffix : "");
- }
- public void setSeparator(String separator) {
- this.separator = separator;
- }
- public void setStripLeadingSlash(boolean stripLeadingSlash) {
- this.stripLeadingSlash = stripLeadingSlash;
- }
- public void setStripTrailingSlash(boolean stripTrailingSlash) {
- this.stripTrailingSlash = stripTrailingSlash;
- }
- public void setStripExtension(boolean stripExtension) {
- this.stripExtension = stripExtension;
- }
- public void setAlwaysUseFullPath(boolean alwaysUseFullPath) {
- this.urlPathHelper.setAlwaysUseFullPath(alwaysUseFullPath);
- }
- public void setUrlDecode(boolean urlDecode) {
- this.urlPathHelper.setUrlDecode(urlDecode);
- }
- public void setRemoveSemicolonContent(boolean removeSemicolonContent) {
- this.urlPathHelper.setRemoveSemicolonContent(removeSemicolonContent);
- }
- public void setUrlPathHelper(UrlPathHelper urlPathHelper) {
- Assert.notNull(urlPathHelper, "UrlPathHelper must not be null");
- this.urlPathHelper = urlPathHelper;
- }
- //根据请求获取视图名称
- @Override
- public String getViewName(HttpServletRequest request) {
- String lookupPath = this.urlPathHelper.getLookupPathForRequest(request);
- return (this.prefix + transformPath(lookupPath) + this.suffix);
- }
- protected String transformPath(String lookupPath) {
- String path = lookupPath;
- if (this.stripLeadingSlash && path.startsWith(SLASH)) {
- path = path.substring(1);
- }
- if (this.stripTrailingSlash && path.endsWith(SLASH)) {
- path = path.substring(0, path.length() - 1);
- }
- if (this.stripExtension) {
- path = StringUtils.stripFilenameExtension(path);
- }
- if (!SLASH.equals(this.separator)) {
- path = StringUtils.replace(path, SLASH, this.separator);
- }
- return path;
- }
- }
数据传递--------博客-----------springMVC源码分析--RequestToViewNameTranslator请求到视图名称的转换的更多相关文章
- springMVC源码分析--RequestToViewNameTranslator请求到视图名称的转换
RequestToViewNameTranslator可以在处理器返回的View为空时使用它根据Request获取viewName.RequestToViewNameTranslator提供的实现类只 ...
- springMVC源码分析--DispatcherServlet请求获取及处理
在之前的博客springMVC源码分析--容器初始化(二)DispatcherServlet中我们介绍过DispatcherServlet,是在容器初始化过程中出现的,我们之前也说过Dispatche ...
- springMVC源码分析--访问请求执行ServletInvocableHandlerMethod和InvocableHandlerMethod
在之前一篇博客中springMVC源码分析--RequestMappingHandlerAdapter(五)我们已经简单的介绍到具体请求访问的执行某个Controller中的方法是在RequestMa ...
- springMVC源码分析--FlashMap和FlashMapManager重定向数据保存
在上一篇博客springMVC源码分析--页面跳转RedirectView(三)中我们看到了在RedirectView跳转时会将跳转之前的请求中的参数保存到fFlashMap中,然后通过FlashMa ...
- springMVC源码分析--HandlerInterceptor拦截器调用过程(二)
在上一篇博客springMVC源码分析--HandlerInterceptor拦截器(一)中我们介绍了HandlerInterceptor拦截器相关的内容,了解到了HandlerInterceptor ...
- springMVC源码分析--页面跳转RedirectView(三)
之前两篇博客springMVC源码分析--视图View(一)和springMVC源码分析--视图AbstractView和InternalResourceView(二)中我们已经简单的介绍了View相 ...
- springMVC源码分析--视图AbstractView和InternalResourceView(二)
上一篇博客springMVC源码分析--视图View(一)中我们介绍了简单介绍了View的结构实现及运行流程,接下来我们介绍一下View的实现类做的处理操作. AbstractView实现了rende ...
- springMVC源码分析--视图View(一)
之前的博客springMVC源码分析--HttpMessageConverter数据转化(一)中我们已经介绍了数据返回值的处理,在博客springMVC源码分析--ViewResolver视图解析器( ...
- springMVC源码分析--HttpMessageConverter写write操作(三)
上一篇博客springMVC源码分析--HttpMessageConverter参数read操作中我们已经简单介绍了参数值转换的read操作,接下来我们介绍一下返回值的处理操作.同样返回值的操作操作也 ...
随机推荐
- Meeting 加虚拟边
Bessie and her friend Elsie decide to have a meeting. However, after Farmer John decorated his fence ...
- Nginx教程收集
学习要系统,最推荐的方式是看书. 下面是收集的一些Nginx教程: https://www.gitbook.com/book/yinsigan/nginx/details http://www.ngi ...
- Android帧动画实现,防OOM,比原生动画集节约超过十倍的资源
2015年项目接到一个需求,实现一个向导动画,这个动画一共六十张图片,当时使用的是全志A33的开发(512的内存),通过使用Android的动画集实现,效果特别卡顿,然后想到这样的方式来实现,效果非常 ...
- (六)Net Core项目使用Controller之一 c# log4net 不输出日志 .NET Standard库引用导致的FileNotFoundException探究 获取json串里的某个属性值 common.js 如何调用common.js js 筛选数据 Join 具体用法
(六)Net Core项目使用Controller之一 一.简介 1.当前最流行的开发模式是前后端分离,Controller作为后端的核心输出,是开发人员使用最多的技术点. 2.个人所在的团队已经选择 ...
- 怎样把引用的jar包和本项目一起导出成jar文件
之所以要导出Runnable JAR.是由于我们希望将引用到的Jar包与本项目一起进行导出,所以不要选Jar file 选File/Export...然后Java/Runnable JAR file, ...
- 异步FIFO的编程
对于异步FIFO.最基本的两个方面是地址控制和空.满标志位的产生.首先地址控制分别为读地址和写地址,每次读写时能读写地址应该加1.计数次数为ram深度的2倍.当读写地址相等时则空标志位有效,当读写地址 ...
- spoj 1812 LCS2 - Longest Common Substring II (后缀自己主动机)
spoj 1812 LCS2 - Longest Common Substring II 题意: 给出最多n个字符串A[1], ..., A[n], 求这n个字符串的最长公共子串. 限制: 1 < ...
- 图片存储系统TFS
1 TFS和GFS比较 1.1 GFS的应用场景 第一,百万级别的文件,并且是大文件,文件都是100MB以上,1G级别的文件很常见. 第二,集群是建立在商业计算机之上,并不可靠,监控各个节点的状态,当 ...
- QQ空间说说 视频播放
http://182.254.8.83/vwecam.gtimg.com/1006_d81d60f3c83844a5ad6a184149d4ccbb.f0.mp4?sha=78A27CF4908AB5 ...
- UVA 213 Message Decoding 【模拟】
题目链接: https://cn.vjudge.net/problem/UVA-213 https://uva.onlinejudge.org/index.php?option=com_onlinej ...