1 24 package org.riotfamily.common.web.view; 25 26 import java.util.Locale ; 27 import java.util.Map ; 28 29 import org.springframework.web.servlet.View; 30 import org.springframework.web.servlet.ViewResolver; 31 import org.springframework.web.servlet.view.AbstractCachingViewResolver; 32 import org.springframework.web.servlet.view.InternalResourceView; 33 import org.springframework.web.servlet.view.RedirectView; 34 35 40 public class SuffixViewResolver extends AbstractCachingViewResolver { 41 42 48 public static final String REDIRECT_URL_PREFIX = "redirect:"; 49 50 56 public static final String FORWARD_URL_PREFIX = "forward:"; 57 58 private Map resolvers; 59 60 private String defaultSuffix; 61 62 private ViewResolver defaultResolver; 63 64 private boolean redirectContextRelative = true; 65 66 private boolean redirectHttp10Compatible = true; 67 68 71 public void setResolvers(Map resolvers) { 72 this.resolvers = resolvers; 73 } 74 75 80 public void setDefaultSuffix(String defaultSuffix) { 81 this.defaultSuffix = defaultSuffix; 82 } 83 84 96 public void setRedirectContextRelative(boolean redirectContextRelative) { 97 this.redirectContextRelative = redirectContextRelative; 98 } 99 100 114 public void setRedirectHttp10Compatible(boolean redirectHttp10Compatible) { 115 this.redirectHttp10Compatible = redirectHttp10Compatible; 116 } 117 118 protected View loadView(String viewName, Locale locale) throws Exception { 119 120 if (viewName.startsWith(REDIRECT_URL_PREFIX)) { 122 String redirectUrl = viewName.substring(REDIRECT_URL_PREFIX.length()); 123 return new RedirectView(redirectUrl, redirectContextRelative, 124 redirectHttp10Compatible); 125 } 126 127 if (viewName.startsWith(FORWARD_URL_PREFIX)) { 129 String forwardUrl = viewName.substring(FORWARD_URL_PREFIX.length()); 130 return new InternalResourceView(forwardUrl); 131 } 132 133 String suffix = null; 134 ViewResolver resolver = null; 135 int i = viewName.lastIndexOf('.'); 136 if (i > 0) { 137 suffix = viewName.substring(i + 1); 138 resolver = (ViewResolver) resolvers.get(suffix); 139 if (logger.isDebugEnabled()) { 140 if (resolver != null) { 141 logger.debug("Using " + resolver + " for suffix [" 142 + suffix + ']'); 143 } 144 } 145 } 146 147 if (resolver == null) { 148 if (suffix != null) { 149 throw new IllegalArgumentException ("No resolver defined for " + 150 "suffix [" + suffix + ']'); 151 } 152 if (defaultSuffix != null) { 153 viewName += "." + defaultSuffix; 154 resolver = defaultResolver; 155 } 156 else { 157 return null; 158 } 159 } 160 return resolver.resolveViewName(viewName, locale); 161 } 162 163 } 164 | Popular Tags |