1 16 17 package org.springframework.web.servlet.view; 18 19 import java.util.Enumeration ; 20 import java.util.Map ; 21 22 import javax.servlet.ServletException ; 23 import javax.servlet.ServletResponse ; 24 import javax.servlet.http.HttpServletRequest ; 25 import javax.servlet.http.HttpServletResponse ; 26 import javax.servlet.http.HttpSession ; 27 28 import org.springframework.web.servlet.support.RequestContext; 29 30 45 public abstract class AbstractTemplateView extends AbstractUrlBasedView { 46 47 51 public static final String SPRING_MACRO_REQUEST_CONTEXT_ATTRIBUTE = "springMacroRequestContext"; 52 53 54 private static boolean responseGetContentTypeAvailable; 55 56 static { 57 try { 60 ServletResponse .class.getMethod("getContentType", new Class [] {}); 61 responseGetContentTypeAvailable = true; 62 } 63 catch (NoSuchMethodException ex) { 64 responseGetContentTypeAvailable = false; 65 } 66 } 67 68 private boolean exposeRequestAttributes = false; 69 70 private boolean exposeSessionAttributes = false; 71 72 private boolean exposeSpringMacroHelpers = false; 73 74 private boolean allowRequestOverride = false; 75 76 private boolean allowSessionOverride = false; 77 78 79 83 public void setExposeRequestAttributes(boolean exposeRequestAttributes) { 84 this.exposeRequestAttributes = exposeRequestAttributes; 85 } 86 87 91 public void setExposeSessionAttributes(boolean exposeSessionAttributes) { 92 this.exposeSessionAttributes = exposeSessionAttributes; 93 } 94 95 101 public void setAllowRequestOverride(boolean allowRequestOverride) { 102 this.allowRequestOverride = allowRequestOverride; 103 } 104 105 111 public void setAllowSessionOverride(boolean allowSessionOverride) { 112 this.allowSessionOverride = allowSessionOverride; 113 } 114 115 123 public void setExposeSpringMacroHelpers(boolean exposeSpringMacroHelpers) { 124 this.exposeSpringMacroHelpers = exposeSpringMacroHelpers; 125 } 126 127 128 protected final void renderMergedOutputModel( 129 Map model, HttpServletRequest request, HttpServletResponse response) throws Exception { 130 131 if (this.exposeRequestAttributes) { 132 for (Enumeration en = request.getAttributeNames(); en.hasMoreElements();) { 133 String attribute = (String ) en.nextElement(); 134 if (model.containsKey(attribute) && !this.allowRequestOverride) { 135 throw new ServletException ("Cannot expose request attribute '" + attribute + 136 "' because of an existing model object of the same name"); 137 } 138 Object attributeValue = request.getAttribute(attribute); 139 if (logger.isDebugEnabled()) { 140 logger.debug("Exposing request attribute '" + attribute + 141 "' with value [" + attributeValue + "] to model"); 142 } 143 model.put(attribute, attributeValue); 144 } 145 } 146 147 if (this.exposeSessionAttributes) { 148 HttpSession session = request.getSession(false); 149 if (session != null) { 150 for (Enumeration en = session.getAttributeNames(); en.hasMoreElements();) { 151 String attribute = (String ) en.nextElement(); 152 if (model.containsKey(attribute) && !this.allowSessionOverride) { 153 throw new ServletException ("Cannot expose session attribute '" + attribute + 154 "' because of an existing model object of the same name"); 155 } 156 Object attributeValue = session.getAttribute(attribute); 157 if (logger.isDebugEnabled()) { 158 logger.debug("Exposing session attribute '" + attribute + 159 "' with value [" + attributeValue + "] to model"); 160 } 161 model.put(attribute, attributeValue); 162 } 163 } 164 } 165 166 if (this.exposeSpringMacroHelpers) { 167 if (model.containsKey(SPRING_MACRO_REQUEST_CONTEXT_ATTRIBUTE)) { 168 throw new ServletException ( 169 "Cannot expose bind macro helper '" + SPRING_MACRO_REQUEST_CONTEXT_ATTRIBUTE + 170 "' because of an existing model object of the same name"); 171 } 172 model.put(SPRING_MACRO_REQUEST_CONTEXT_ATTRIBUTE, new RequestContext(request, model)); 174 } 175 176 applyContentType(response); 177 178 renderMergedTemplateModel(model, request, response); 179 } 180 181 190 protected void applyContentType(HttpServletResponse response) { 191 if (!responseGetContentTypeAvailable || response.getContentType() == null) { 192 response.setContentType(getContentType()); 193 } 194 } 195 196 204 protected abstract void renderMergedTemplateModel( 205 Map model, HttpServletRequest request, HttpServletResponse response) throws Exception ; 206 207 } 208 | Popular Tags |