作用:将request请求转换为视图名称,比如转换为 index.jsp.
可以看到这个接口就一个实现类DefaultRequestToViewNameTranslator下面直接看这个类实现的接口中的方法String getViewName(HttpServletRequest request)
。
public String getViewName(HttpServletRequest request) {
//主要是委派给urlPathHelper帮助类得到请求的后缀名称,比如通过 请求路径/webname/login.do转换得到/login.do
String lookupPath = this.urlPathHelper.getLookupPathForRequest(request);//返回/login.do
return (this.prefix + transformPath(lookupPath) + this.suffix);
}
下面看UrlPathHelper类中的getLookupPathForRequest(request)方法。主要代码如下:
……………………
String contextPath = getContextPath(request);// /webname
String requestUri = getRequestUri(request);// /webname/login.do
if (StringUtils.startsWithIgnoreCase(requestUri, contextPath)) {
// Normal case: URI contains context path.
String path = requestUri.substring(contextPath.length());// /login.do
return (StringUtils.hasText(path) ? path : "/");
}
……………………
然后在看transformPath(lookupPath)方法。
……………………
//去掉前缀/
if (this.stripLeadingSlash && path.startsWith(SLASH)) {
path = path.substring(1);
}
//去掉后缀/
if (this.stripTrailingSlash && path.endsWith(SLASH)) {
path = path.substring(0, path.length() - 1);
}
//去掉结尾.包括.后面的后缀,比如把login.do中的 . 去掉,变为login
if (this.stripExtension) {
path = StringUtils.stripFilenameExtension(path);
}
……………………
通过上面的一系列操作,就可以根据request解析得到需要的view名称。比如login.do转换为login.jsp