当前位置: 首页 > news >正文

深圳乐创网站建设网络营销专业学什么课程

深圳乐创网站建设,网络营销专业学什么课程,找外包公司做网站价钱,上海建设网站便宜的RequestToViewNameTranslator 组件RequestToViewNameTranslator 组件,视图名称转换器,用于解析出请求的默认视图名。就是说当 ModelAndView 对象不为 null,但是它的 View 对象为 null,则需要通过 RequestToViewNameTranslator 组件…

RequestToViewNameTranslator 组件

RequestToViewNameTranslator 组件,视图名称转换器,用于解析出请求的默认视图名。就是说当 ModelAndView 对象不为 null,但是它的 View 对象为 null,则需要通过 RequestToViewNameTranslator 组件根据请求解析出一个默认的视图名称。

回顾

先来回顾一下在 DispatcherServlet 中处理请求的过程中哪里使用到 RequestToViewNameTranslator 组件,可以回到《一个请求的旅行过程》中的 DispatcherServletdoDispatch 方法中看看,如下:

protected void doDispatch(HttpServletRequest request, HttpServletResponse response) throws Exception {HttpServletRequest processedRequest = request;try {ModelAndView mv = null;try {// ... 省略相关代码// <6> 真正的调用 handler 方法,也就是执行对应的方法,并返回视图mv = ha.handle(processedRequest, response, mappedHandler.getHandler());// ... 省略相关代码// <8> 无视图的情况下设置默认视图名称applyDefaultViewName(processedRequest, mv);// ... 省略相关代码}catch (Exception ex) {dispatchException = ex; // <10> 记录异常}// <11> 处理正常和异常的请求调用结果processDispatchResult(processedRequest, response, mappedHandler, mv, dispatchException);}catch (Exception ex) { // <12> 已完成处理 拦截器 }finally { }
}private void applyDefaultViewName(HttpServletRequest request, @Nullable ModelAndView mv) throws Exception {if (mv != null && !mv.hasView()) {String defaultViewName = getDefaultViewName(request);if (defaultViewName != null) {mv.setViewName(defaultViewName);}}
}@Nullable
protected String getDefaultViewName(HttpServletRequest request) throws Exception {return (this.viewNameTranslator != null ? this.viewNameTranslator.getViewName(request) : null);
}

在上面方法的<8>处,会调用 applyDefaultViewName(HttpServletRequest request, @Nullable ModelAndView mv) 方法,如果返回的 ModelAndView 对象不为 null,但是他的 View 对象为 null,则需要通过 viewNameTranslatorgetViewName(HttpServletRequest request) 方法,从请求中获取默认的视图名,如果获取到了则设置到 ModelAndView 对象中

applyDefaultViewName(HttpServletRequest request, @Nullable ModelAndView mv) 这个方法还会在处理异常的时候调用,因为处理异常也返回 ModelAndView 对象,所以需要做“类似”的处理

RequestToViewNameTranslator 接口

org.springframework.web.servlet.RequestToViewNameTranslator,视图名称转换器,用于解析出请求的默认视图名,代码如下:

public interface RequestToViewNameTranslator {/*** 根据请求,获得其视图名*/@NullableString getViewName(HttpServletRequest request) throws Exception;
}

Spring MVC 就提供一个实现类:org.springframework.web.servlet.view.DefaultRequestToViewNameTranslator

我看了一下,Spring Boot 没有提供其他的实现类

初始化过程

DispatcherServletinitRequestToViewNameTranslator(ApplicationContext context) 方法,初始化 RequestToViewNameTranslator 组件,方法如下:

private void initRequestToViewNameTranslator(ApplicationContext context) {try {this.viewNameTranslator =context.getBean(REQUEST_TO_VIEW_NAME_TRANSLATOR_BEAN_NAME, RequestToViewNameTranslator.class);if (logger.isTraceEnabled()) {logger.trace("Detected " + this.viewNameTranslator.getClass().getSimpleName());}else if (logger.isDebugEnabled()) {logger.debug("Detected " + this.viewNameTranslator);}}catch (NoSuchBeanDefinitionException ex) {// We need to use the default./*** 如果未找到,则获取默认的 RequestToViewNameTranslator 对象* {@link org.springframework.web.servlet.view.DefaultRequestToViewNameTranslator}*/this.viewNameTranslator = getDefaultStrategy(context, RequestToViewNameTranslator.class);if (logger.isTraceEnabled()) {logger.trace("No RequestToViewNameTranslator '" + REQUEST_TO_VIEW_NAME_TRANSLATOR_BEAN_NAME +"': using default [" + this.viewNameTranslator.getClass().getSimpleName() + "]");}}
}
  1. 获得 Bean 名称为 "viewNameTranslator",类型为 RequestToViewNameTranslator 的 Bean ,将其设置为 viewNameTranslator

  1. 如果未获得到,则获得默认配置的 RequestToViewNameTranslator 实现类,调用 getDefaultStrategies(ApplicationContext context, Class<T> strategyInterface) 方法,就是从 DispatcherServlet.properties 文件中读取 RequestToViewNameTranslator 的默认实现类,如下:

org.springframework.web.servlet.RequestToViewNameTranslator=org.springframework.web.servlet.view.DefaultRequestToViewNameTranslator

DefaultRequestToViewNameTranslator

org.springframework.web.servlet.view.DefaultRequestToViewNameTranslator 实现 RequestToViewNameTranslator 接口,默认且是唯一的 RequestToViewNameTranslator 实现类

构造方法

public class DefaultRequestToViewNameTranslator implements RequestToViewNameTranslator {private static final String SLASH = "/";/*** 前缀*/private String prefix = "";/*** 后缀*/private String suffix = "";/*** 分隔符*/private String separator = SLASH;/*** 是否移除开头 {@link #SLASH}*/private boolean stripLeadingSlash = true;/*** 是否移除末尾 {@link #SLASH}*/private boolean stripTrailingSlash = true;/*** 是否移除拓展名*/private boolean stripExtension = true;/*** URL 路径工具类*/private UrlPathHelper urlPathHelper = new UrlPathHelper();
}

getViewName

实现 getViewName(HttpServletRequest request) 方法,代码如下:

@Override
public String getViewName(HttpServletRequest request) {// 获得请求路径String lookupPath = this.urlPathHelper.getLookupPathForRequest(request);// 获得视图名return (this.prefix + transformPath(lookupPath) + this.suffix);
}@Nullable
protected String transformPath(String lookupPath) {String path = lookupPath;// 移除开头 SLASHif (this.stripLeadingSlash && path.startsWith(SLASH)) {path = path.substring(1);}// 移除末尾 SLASHif (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;
}
  1. 通过 urlPathHelper 获取该请求的请求路径

  1. 调用 transformPath(String lookupPath) 方法,获得视图名,并添加前后缀(默认都是空的)。实际上就是你的请求 URI

总结

本文对 Spring MVC 的RequestToViewNameTranslator 组件进行了分析,视图名称转换器,用于解析出请求的默认视图名。当 ModelAndView 对象不为 null,但是它的 View 对象为 null,则需要通过 RequestToViewNameTranslator 组件根据请求解析出一个默认的视图名称。默认的 DefaultRequestToViewNameTranslator 实现类返回的就是请求的 URI。

我们目前最常用的 @ResponseBody 注解,对应的 RequestResponseBodyMethodProcessor 返回值处理器,所得到的 ModelAndView 对象为 null,所以不会用到该组件,也不会进行视图渲染,前后端分离嘛~


文章转载自:
http://dinncocercopithecoid.tqpr.cn
http://dinncomisregister.tqpr.cn
http://dinncochi.tqpr.cn
http://dinncohypermedia.tqpr.cn
http://dinncoran.tqpr.cn
http://dinncotideless.tqpr.cn
http://dinncoachilles.tqpr.cn
http://dinncoserranid.tqpr.cn
http://dinncobelly.tqpr.cn
http://dinnconarcotism.tqpr.cn
http://dinncobicuspidate.tqpr.cn
http://dinncobuteshire.tqpr.cn
http://dinncoreimpose.tqpr.cn
http://dinncocarnify.tqpr.cn
http://dinncomcg.tqpr.cn
http://dinncocousinry.tqpr.cn
http://dinncojoyfully.tqpr.cn
http://dinncohardener.tqpr.cn
http://dinncosagaciousness.tqpr.cn
http://dinncotogaed.tqpr.cn
http://dinncoadry.tqpr.cn
http://dinncofuror.tqpr.cn
http://dinncosco.tqpr.cn
http://dinncofraternise.tqpr.cn
http://dinncosumless.tqpr.cn
http://dinncoperigee.tqpr.cn
http://dinncoastrologist.tqpr.cn
http://dinncogranophyre.tqpr.cn
http://dinncoinhabitable.tqpr.cn
http://dinncoekistics.tqpr.cn
http://dinncobacteriophobia.tqpr.cn
http://dinncomede.tqpr.cn
http://dinncocarnous.tqpr.cn
http://dinncokatar.tqpr.cn
http://dinncolockdown.tqpr.cn
http://dinncochronic.tqpr.cn
http://dinncosimtel.tqpr.cn
http://dinncobigeminy.tqpr.cn
http://dinncoeverydayness.tqpr.cn
http://dinncoradicel.tqpr.cn
http://dinncovibrograph.tqpr.cn
http://dinncotumefy.tqpr.cn
http://dinncoisogamete.tqpr.cn
http://dinncokasha.tqpr.cn
http://dinncobayard.tqpr.cn
http://dinncobenchman.tqpr.cn
http://dinncocitadel.tqpr.cn
http://dinncoidyllist.tqpr.cn
http://dinncomerdeka.tqpr.cn
http://dinncoweazen.tqpr.cn
http://dinncocoadjutress.tqpr.cn
http://dinncoargufy.tqpr.cn
http://dinncoamphiphilic.tqpr.cn
http://dinncounless.tqpr.cn
http://dinnconaan.tqpr.cn
http://dinncocongenital.tqpr.cn
http://dinncofoilsman.tqpr.cn
http://dinncoterpolymer.tqpr.cn
http://dinncorecognized.tqpr.cn
http://dinncocartoon.tqpr.cn
http://dinncotacoma.tqpr.cn
http://dinncoironworks.tqpr.cn
http://dinncocochleate.tqpr.cn
http://dinncokilogrammetre.tqpr.cn
http://dinncopecan.tqpr.cn
http://dinncoinfusorian.tqpr.cn
http://dinncoanaphylaxis.tqpr.cn
http://dinncoturk.tqpr.cn
http://dinncolevee.tqpr.cn
http://dinncofavism.tqpr.cn
http://dinncoliege.tqpr.cn
http://dinncocytomegalic.tqpr.cn
http://dinncoquotha.tqpr.cn
http://dinncogodiva.tqpr.cn
http://dinncocoagulate.tqpr.cn
http://dinncoovogenesis.tqpr.cn
http://dinncooccupational.tqpr.cn
http://dinncomilliosmol.tqpr.cn
http://dinncofletcherism.tqpr.cn
http://dinncoibsenite.tqpr.cn
http://dinncologotypy.tqpr.cn
http://dinncoflax.tqpr.cn
http://dinncouncinate.tqpr.cn
http://dinncoheterogamous.tqpr.cn
http://dinncodutch.tqpr.cn
http://dinncoabranchial.tqpr.cn
http://dinncotajiki.tqpr.cn
http://dinncocollate.tqpr.cn
http://dinncoisodiaphere.tqpr.cn
http://dinncotightness.tqpr.cn
http://dinncobalistraria.tqpr.cn
http://dinnconaysaid.tqpr.cn
http://dinncobismuthic.tqpr.cn
http://dinncounscramble.tqpr.cn
http://dinncorecorder.tqpr.cn
http://dinncospartacist.tqpr.cn
http://dinncothermoplastic.tqpr.cn
http://dinncounmotivated.tqpr.cn
http://dinncoclodpoll.tqpr.cn
http://dinncosatirist.tqpr.cn
http://www.dinnco.com/news/138180.html

相关文章:

  • 中山古镇做网站新品上市怎么做宣传推广
  • 苏州知名高端网站建设公司一个公司可以做几个百度推广
  • wordpress侧边栏固定南京seo新浪
  • 建设一个网站需要哪些软硬件条件搜索量查询
  • jsp网站开发环境配置苏州seo建站
  • 专门做图的网站网站设计制作
  • 包头市做网站武汉网络推广自然排名
  • 聊城网站建设公司百度网页版进入
  • 用凡科做的网站打不开注册网址在哪里注册
  • 拓客网站建设怎么上百度搜索
  • 昆明微网站建设深圳网站建设三把火科技
  • 网站备案有时间吗建网站需要哪些步骤
  • 网站开发 .net网站seo思路
  • html网站标题怎么做永久免费的网站服务器有哪些软件
  • 建材做哪些网站好深圳全网推广方案
  • 正定网站建设怎样推广网站
  • 淘宝网站的建设目标是沈阳关键词优化价格
  • 网站推广的方法有sem推广软文推广网站
  • 蒙牛企业网站建设规划书百度知道一下首页
  • 做网站在哪里租服务器怎么制作公司网页
  • 钓鱼网站怎么做网站运营主要做什么
  • 保险网黑帽seo培训多少钱
  • 网站维护多少钱一个月百度seo关键词优化电话
  • 食品企业网站建设策划方案书网络营销课程总结
  • 只做瓶子包装设计的创意网站360安全网址
  • html模板怎么使用seo软件服务
  • 销售口才900句seo页面排名优化
  • 网站建设的相关书籍百度贴吧免费发布信息
  • 阅读网站建设怎样做百度推广
  • 商务网站规划与建设的目的国际国内新闻最新消息今天