1 16 17 package org.springframework.web.servlet.view; 18 19 import java.util.Collections ; 20 import java.util.HashMap ; 21 import java.util.Iterator ; 22 import java.util.Map ; 23 import java.util.Properties ; 24 import java.util.StringTokenizer ; 25 26 import javax.servlet.http.HttpServletRequest ; 27 import javax.servlet.http.HttpServletResponse ; 28 29 import org.springframework.beans.factory.BeanNameAware; 30 import org.springframework.util.CollectionUtils; 31 import org.springframework.web.context.support.WebApplicationObjectSupport; 32 import org.springframework.web.servlet.View; 33 import org.springframework.web.servlet.support.RequestContext; 34 35 54 public abstract class AbstractView extends WebApplicationObjectSupport implements View, BeanNameAware { 55 56 57 public static final String DEFAULT_CONTENT_TYPE = "text/html; charset=ISO-8859-1"; 58 59 60 private String beanName; 61 62 private String contentType = DEFAULT_CONTENT_TYPE; 63 64 private String requestContextAttribute; 65 66 67 private final Map staticAttributes = new HashMap (); 68 69 70 76 public void setBeanName(String beanName) { 77 this.beanName = beanName; 78 } 79 80 84 public String getBeanName() { 85 return beanName; 86 } 87 88 96 public void setContentType(String contentType) { 97 this.contentType = contentType; 98 } 99 100 103 public String getContentType() { 104 return this.contentType; 105 } 106 107 111 public void setRequestContextAttribute(String requestContextAttribute) { 112 this.requestContextAttribute = requestContextAttribute; 113 } 114 115 118 public String getRequestContextAttribute() { 119 return this.requestContextAttribute; 120 } 121 122 126 public void setAttributesCSV(String propString) throws IllegalArgumentException { 127 if (propString != null) { 128 StringTokenizer st = new StringTokenizer (propString, ","); 129 while (st.hasMoreTokens()) { 130 String tok = st.nextToken(); 131 int eqIdx = tok.indexOf("="); 132 if (eqIdx == -1) { 133 throw new IllegalArgumentException ("Expected = in attributes CSV string '" + propString + "'"); 134 } 135 if (eqIdx >= tok.length() - 2) { 136 throw new IllegalArgumentException ( 137 "At least 2 characters ([]) required in attributes CSV string '" + propString + "'"); 138 } 139 String name = tok.substring(0, eqIdx); 140 String value = tok.substring(eqIdx + 1); 141 142 value = value.substring(1); 144 value = value.substring(0, value.length() - 1); 145 146 addStaticAttribute(name, value); 147 } 148 } 149 } 150 151 161 public void setAttributes(Properties attributes) { 162 CollectionUtils.mergePropertiesIntoMap(attributes, this.staticAttributes); 163 } 164 165 171 public void setAttributesMap(Map attributes) { 172 if (attributes != null) { 173 Iterator it = attributes.entrySet().iterator(); 174 while (it.hasNext()) { 175 Map.Entry entry = (Map.Entry ) it.next(); 176 Object key = entry.getKey(); 177 if (!(key instanceof String )) { 178 throw new IllegalArgumentException ( 179 "Invalid attribute key [" + key + "]: only Strings allowed"); 180 } 181 addStaticAttribute((String ) key, entry.getValue()); 182 } 183 } 184 } 185 186 193 public Map getAttributesMap() { 194 return this.staticAttributes; 195 } 196 197 204 public void addStaticAttribute(String name, Object value) { 205 this.staticAttributes.put(name, value); 206 if (logger.isDebugEnabled()) { 207 logger.debug("Set static attribute with name '" + name + "' and value [" + value + 208 "] on view with name '" + getBeanName() + "'"); 209 } 210 } 211 212 218 public Map getStaticAttributes() { 219 return Collections.unmodifiableMap(this.staticAttributes); 220 } 221 222 223 229 public void render(Map model, HttpServletRequest request, HttpServletResponse response) throws Exception { 230 if (logger.isDebugEnabled()) { 231 logger.debug("Rendering view with name '" + this.beanName + "' with model " + model + 232 " and static attributes " + this.staticAttributes); 233 } 234 235 Map mergedModel = new HashMap (this.staticAttributes.size() + (model != null ? model.size() : 0)); 237 mergedModel.putAll(this.staticAttributes); 238 if (model != null) { 239 mergedModel.putAll(model); 240 } 241 242 if (this.requestContextAttribute != null) { 244 mergedModel.put(this.requestContextAttribute, createRequestContext(request, mergedModel)); 245 } 246 247 renderMergedOutputModel(mergedModel, request, response); 248 } 249 250 261 protected RequestContext createRequestContext(HttpServletRequest request, Map model) { 262 return new RequestContext(request, model); 263 } 264 265 277 protected abstract void renderMergedOutputModel( 278 Map model, HttpServletRequest request, HttpServletResponse response) throws Exception ; 279 280 281 288 protected void exposeModelAsRequestAttributes(Map model, HttpServletRequest request) throws Exception { 289 Iterator it = model.entrySet().iterator(); 290 while (it.hasNext()) { 291 Map.Entry entry = (Map.Entry ) it.next(); 292 if (!(entry.getKey() instanceof String )) { 293 throw new IllegalArgumentException ( 294 "Invalid key [" + entry.getKey() + "] in model Map: only Strings allowed as model keys"); 295 } 296 String modelName = (String ) entry.getKey(); 297 Object modelValue = entry.getValue(); 298 if (modelValue != null) { 299 request.setAttribute(modelName, modelValue); 300 if (logger.isDebugEnabled()) { 301 logger.debug("Added model object '" + modelName + "' of type [" + modelValue.getClass().getName() + 302 "] to request in view with name '" + getBeanName() + "'"); 303 } 304 } 305 else { 306 request.removeAttribute(modelName); 307 if (logger.isDebugEnabled()) { 308 logger.debug("Removed model object '" + modelName + 309 "' from request in view with name '" + getBeanName() + "'"); 310 } 311 } 312 } 313 } 314 315 316 public String toString() { 317 StringBuffer sb = new StringBuffer (getClass().getName()); 318 if (getBeanName() != null) { 319 sb.append(": name '").append(getBeanName()).append("'"); 320 } 321 else { 322 sb.append(": unnamed"); 323 } 324 return sb.toString(); 325 } 326 327 } 328 | Popular Tags |