1 16 17 package org.springframework.web.servlet.view; 18 19 import java.util.HashMap ; 20 import java.util.Locale ; 21 import java.util.Map ; 22 import java.util.Properties ; 23 24 import org.springframework.beans.BeanUtils; 25 import org.springframework.core.Ordered; 26 import org.springframework.util.PatternMatchUtils; 27 import org.springframework.web.servlet.View; 28 29 80 public class UrlBasedViewResolver extends AbstractCachingViewResolver implements Ordered { 81 82 88 public static final String REDIRECT_URL_PREFIX = "redirect:"; 89 90 96 public static final String FORWARD_URL_PREFIX = "forward:"; 97 98 99 private Class viewClass; 100 101 private String prefix = ""; 102 103 private String suffix = ""; 104 105 private String [] viewNames = null; 106 107 private String contentType; 108 109 private boolean redirectContextRelative = true; 110 111 private boolean redirectHttp10Compatible = true; 112 113 private String requestContextAttribute; 114 115 private int order = Integer.MAX_VALUE; 116 117 118 private final Map staticAttributes = new HashMap (); 119 120 121 127 public void setViewClass(Class viewClass) { 128 if (viewClass == null || !requiredViewClass().isAssignableFrom(viewClass)) { 129 throw new IllegalArgumentException ( 130 "Given view class [" + (viewClass != null ? viewClass.getName() : null) + 131 "] is not of type [" + requiredViewClass().getName() + "]"); 132 } 133 this.viewClass = viewClass; 134 } 135 136 139 protected Class getViewClass() { 140 return this.viewClass; 141 } 142 143 148 protected Class requiredViewClass() { 149 return AbstractUrlBasedView.class; 150 } 151 152 155 public void setPrefix(String prefix) { 156 this.prefix = (prefix != null ? prefix : ""); 157 } 158 159 162 protected String getPrefix() { 163 return prefix; 164 } 165 166 169 public void setSuffix(String suffix) { 170 this.suffix = (suffix != null ? suffix : ""); 171 } 172 173 176 protected String getSuffix() { 177 return suffix; 178 } 179 180 185 public void setContentType(String contentType) { 186 this.contentType = contentType; 187 } 188 189 192 protected String getContentType() { 193 return contentType; 194 } 195 196 208 public void setRedirectContextRelative(boolean redirectContextRelative) { 209 this.redirectContextRelative = redirectContextRelative; 210 } 211 212 217 protected boolean isRedirectContextRelative() { 218 return redirectContextRelative; 219 } 220 221 235 public void setRedirectHttp10Compatible(boolean redirectHttp10Compatible) { 236 this.redirectHttp10Compatible = redirectHttp10Compatible; 237 } 238 239 242 protected boolean isRedirectHttp10Compatible() { 243 return redirectHttp10Compatible; 244 } 245 246 251 public void setRequestContextAttribute(String requestContextAttribute) { 252 this.requestContextAttribute = requestContextAttribute; 253 } 254 255 258 protected String getRequestContextAttribute() { 259 return requestContextAttribute; 260 } 261 262 273 public void setAttributes(Properties props) { 274 setAttributesMap(props); 275 } 276 277 284 public void setAttributesMap(Map attributes) { 285 if (attributes != null) { 286 this.staticAttributes.putAll(attributes); 287 } 288 } 289 290 297 public Map getAttributesMap() { 298 return this.staticAttributes; 299 } 300 301 308 public void setViewNames(String [] viewNames) { 309 this.viewNames = viewNames; 310 } 311 312 316 protected String [] getViewNames() { 317 return viewNames; 318 } 319 320 324 public void setOrder(int order) { 325 this.order = order; 326 } 327 328 332 public int getOrder() { 333 return order; 334 } 335 336 protected void initApplicationContext() { 337 super.initApplicationContext(); 338 if (getViewClass() == null) { 339 throw new IllegalArgumentException ("viewClass is required"); 340 } 341 } 342 343 344 348 protected Object getCacheKey(String viewName, Locale locale) { 349 return viewName; 350 } 351 352 360 protected View createView(String viewName, Locale locale) throws Exception { 361 if (!canHandle(viewName, locale)) { 364 return null; 365 } 366 if (viewName.startsWith(REDIRECT_URL_PREFIX)) { 368 String redirectUrl = viewName.substring(REDIRECT_URL_PREFIX.length()); 369 return new RedirectView( 370 redirectUrl, isRedirectContextRelative(), isRedirectHttp10Compatible()); 371 } 372 if (viewName.startsWith(FORWARD_URL_PREFIX)) { 374 String forwardUrl = viewName.substring(FORWARD_URL_PREFIX.length()); 375 return new InternalResourceView(forwardUrl); 376 } 377 return super.createView(viewName, locale); 379 } 380 381 388 protected boolean canHandle(String viewName, Locale locale) { 389 String [] viewNames = getViewNames(); 390 return (viewNames == null || PatternMatchUtils.simpleMatch(viewNames, viewName)); 391 } 392 393 405 protected View loadView(String viewName, Locale locale) throws Exception { 406 AbstractUrlBasedView view = buildView(viewName); 407 return (View) getApplicationContext().getAutowireCapableBeanFactory().initializeBean(view, viewName); 408 } 409 410 421 protected AbstractUrlBasedView buildView(String viewName) throws Exception { 422 AbstractUrlBasedView view = (AbstractUrlBasedView) BeanUtils.instantiateClass(getViewClass()); 423 view.setUrl(getPrefix() + viewName + getSuffix()); 424 String contentType = getContentType(); 425 if (contentType != null) { 426 view.setContentType(contentType); 427 } 428 view.setRequestContextAttribute(getRequestContextAttribute()); 429 view.setAttributesMap(getAttributesMap()); 430 return view; 431 } 432 433 } 434 | Popular Tags |