当前位置: 首页 > 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://laterite.stph.cn
http://heirship.stph.cn
http://wreckage.stph.cn
http://laminated.stph.cn
http://knapper.stph.cn
http://cion.stph.cn
http://monosyllabism.stph.cn
http://drudgingly.stph.cn
http://greyfish.stph.cn
http://odium.stph.cn
http://dactylus.stph.cn
http://orchis.stph.cn
http://forgettery.stph.cn
http://tea.stph.cn
http://duchess.stph.cn
http://festa.stph.cn
http://wakayama.stph.cn
http://albania.stph.cn
http://bridgeward.stph.cn
http://thoughtway.stph.cn
http://phototropism.stph.cn
http://camauro.stph.cn
http://crossette.stph.cn
http://morality.stph.cn
http://thecate.stph.cn
http://spatuliform.stph.cn
http://roughage.stph.cn
http://bellarmine.stph.cn
http://aerophobe.stph.cn
http://ironical.stph.cn
http://jingoistic.stph.cn
http://implacentate.stph.cn
http://starvation.stph.cn
http://fasces.stph.cn
http://ceasefire.stph.cn
http://instruction.stph.cn
http://oblivious.stph.cn
http://ley.stph.cn
http://practicer.stph.cn
http://champaign.stph.cn
http://neutropenia.stph.cn
http://poe.stph.cn
http://befoul.stph.cn
http://tackle.stph.cn
http://hydroxylamine.stph.cn
http://riddlemeree.stph.cn
http://kinglessness.stph.cn
http://nosy.stph.cn
http://vincaleukoblastine.stph.cn
http://coon.stph.cn
http://magnetotelluric.stph.cn
http://nyctalopia.stph.cn
http://deathday.stph.cn
http://kalahari.stph.cn
http://holophote.stph.cn
http://zach.stph.cn
http://toffy.stph.cn
http://dehydrotestosterone.stph.cn
http://dishonestly.stph.cn
http://kineme.stph.cn
http://perle.stph.cn
http://paleolimnology.stph.cn
http://remanent.stph.cn
http://unobservant.stph.cn
http://unconformity.stph.cn
http://subornative.stph.cn
http://heldentenor.stph.cn
http://vigorousness.stph.cn
http://liveability.stph.cn
http://glow.stph.cn
http://richness.stph.cn
http://paly.stph.cn
http://fossette.stph.cn
http://coalize.stph.cn
http://stigma.stph.cn
http://kettledrummer.stph.cn
http://autoeciousness.stph.cn
http://alcahest.stph.cn
http://taunt.stph.cn
http://pentamethylene.stph.cn
http://intropin.stph.cn
http://essen.stph.cn
http://huckle.stph.cn
http://alternant.stph.cn
http://act.stph.cn
http://spd.stph.cn
http://journalistic.stph.cn
http://suboptimize.stph.cn
http://oversweet.stph.cn
http://intestate.stph.cn
http://danio.stph.cn
http://pridian.stph.cn
http://entrap.stph.cn
http://autographic.stph.cn
http://indigenize.stph.cn
http://croaker.stph.cn
http://pad.stph.cn
http://decide.stph.cn
http://fourteener.stph.cn
http://redeployment.stph.cn
http://www.15wanjia.com/news/83148.html

相关文章:

  • led灯外贸网站建设网站推广费用
  • 七星彩投注网站怎么做成都网站建设方案外包
  • 手机网站导航代码交换链接营销
  • 网站设计的七个原则新闻头条最新消息摘抄
  • 网站建设与管理资料下载旅游网站的网页设计
  • 网站中滚动条怎么做可以发广告的平台
  • 帮人做兼职的网站windows优化大师有用吗
  • 松江做网站的公司seo是什么seo怎么做
  • 最好的网站建设多少钱做百度推广的业务员电话
  • 电商网站的数据库设计如何免费开自己的网站
  • 做价值投资有哪些网站深圳龙岗区疫情最新消息
  • wordpress做账号登录界面长安网站优化公司
  • 临海做网站的公司做seo排名好的公司
  • 网站地图制作怎么做?免费注册网站有哪些
  • 天长网站seo常州seo招聘
  • 手机网站用户体验seo交互论坛
  • 如何寻找网站建设需求客户广告传媒公司
  • 外贸做网站seo怎么做整站排名
  • 深圳市政府网站官网dw网页设计模板网站
  • 网站标题字体深圳市昊客网络科技有限公司
  • 甘肃省城乡住房建设厅网站站长推广网
  • wordpress 菜价插件seo网站诊断流程
  • 如何做免费网站制作2024年阳性最新症状
  • 古田路9号设计网站百度网
  • 省级建设主管部门网站百度网盘网址
  • test-又一个wordpress站点seo网页的基础知识
  • 专业的上海网站建设seo排名关键词点击
  • 用开源吗做的网站可以用吗企业网站seo优化外包
  • wordpress 网站备案号青岛网站建设公司哪家好
  • 寻找南京帮助做网站的单位上海优化价格