1 16 17 package org.springframework.web.servlet.view; 18 19 import javax.servlet.http.HttpServletRequest ; 20 21 import org.springframework.util.StringUtils; 22 import org.springframework.util.Assert; 23 import org.springframework.web.servlet.RequestToViewNameTranslator; 24 import org.springframework.web.util.UrlPathHelper; 25 26 55 public class DefaultRequestToViewNameTranslator implements RequestToViewNameTranslator { 56 57 private static final String SLASH = "/"; 58 59 60 private String prefix = ""; 61 62 private String suffix = ""; 63 64 private String separator = SLASH; 65 66 private boolean stripLeadingSlash = true; 67 68 private boolean stripExtension = true; 69 70 private UrlPathHelper urlPathHelper = new UrlPathHelper(); 71 72 73 77 public void setPrefix(String prefix) { 78 this.prefix = (prefix == null ? "" : prefix); 79 } 80 81 85 public void setSuffix(String suffix) { 86 this.suffix = (suffix == null ? "" : suffix); 87 } 88 89 95 public void setSeparator(String separator) { 96 this.separator = separator; 97 } 98 99 104 public void setStripLeadingSlash(boolean stripLeadingSlash) { 105 this.stripLeadingSlash = stripLeadingSlash; 106 } 107 108 113 public void setStripExtension(boolean stripExtension) { 114 this.stripExtension = stripExtension; 115 } 116 117 125 public void setAlwaysUseFullPath(boolean alwaysUseFullPath) { 126 this.urlPathHelper.setAlwaysUseFullPath(alwaysUseFullPath); 127 } 128 129 141 public void setUrlDecode(boolean urlDecode) { 142 this.urlPathHelper.setUrlDecode(urlDecode); 143 } 144 145 153 public void setUrlPathHelper(UrlPathHelper urlPathHelper) { 154 Assert.notNull(urlPathHelper); 155 this.urlPathHelper = urlPathHelper; 156 } 157 158 159 165 public final String getViewName(HttpServletRequest request) { 166 String lookupPath = this.urlPathHelper.getLookupPathForRequest(request); 167 return this.prefix + transformPath(lookupPath) + this.suffix; 168 } 169 170 171 179 protected String transformPath(String lookupPath) { 180 String path = lookupPath; 181 if (this.stripLeadingSlash && path.startsWith(SLASH)) { 182 path = path.substring(1); 183 } 184 if (this.stripExtension) { 185 path = StringUtils.stripFilenameExtension(path); 186 } 187 if (!SLASH.equals(this.separator)) { 188 path = StringUtils.replace(path, SLASH, this.separator); 189 } 190 return path; 191 } 192 193 } 194 | Popular Tags |