1 16 17 package org.springframework.web.servlet.mvc; 18 19 import java.util.Collections ; 20 import java.util.HashMap ; 21 import java.util.Map ; 22 23 import javax.servlet.http.HttpServletRequest ; 24 25 import org.springframework.util.StringUtils; 26 import org.springframework.web.servlet.HandlerMapping; 27 28 52 public class UrlFilenameViewController extends AbstractUrlViewController { 53 54 private String prefix = ""; 55 56 private String suffix = ""; 57 58 59 private final Map viewNameCache = Collections.synchronizedMap(new HashMap ()); 60 61 62 66 public void setPrefix(String prefix) { 67 this.prefix = (prefix != null ? prefix : ""); 68 } 69 70 73 protected String getPrefix() { 74 return this.prefix; 75 } 76 77 81 public void setSuffix(String suffix) { 82 this.suffix = (suffix != null ? suffix : ""); 83 } 84 85 88 protected String getSuffix() { 89 return this.suffix; 90 } 91 92 93 100 protected String getViewNameForRequest(HttpServletRequest request) { 101 String uri = extractOperableUrl(request); 102 return getViewNameForUrlPath(uri); 103 } 104 105 111 protected String extractOperableUrl(HttpServletRequest request) { 112 String urlPath = (String ) request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE); 113 if (!StringUtils.hasText(urlPath)) { 114 urlPath = getUrlPathHelper().getLookupPathForRequest(request); 115 } 116 return urlPath; 117 } 118 119 127 protected String getViewNameForUrlPath(String uri) { 128 String viewName = (String ) this.viewNameCache.get(uri); 129 if (viewName == null) { 130 viewName = extractViewNameFromUrlPath(uri); 131 viewName = postProcessViewName(viewName); 132 this.viewNameCache.put(uri, viewName); 133 } 134 return viewName; 135 } 136 137 142 protected String extractViewNameFromUrlPath(String uri) { 143 int start = (uri.charAt(0) == '/' ? 1 : 0); 144 int lastIndex = uri.lastIndexOf("."); 145 int end = (lastIndex < 0 ? uri.length() : lastIndex); 146 return uri.substring(start, end); 147 } 148 149 160 protected String postProcessViewName(String viewName) { 161 return getPrefix() + viewName + getSuffix(); 162 } 163 164 } 165 | Popular Tags |