1 16 17 package org.springframework.web.servlet.view.tiles; 18 19 import javax.servlet.ServletException ; 20 import javax.servlet.http.HttpServletRequest ; 21 import javax.servlet.http.HttpServletResponse ; 22 23 import org.apache.struts.tiles.ComponentContext; 24 import org.apache.struts.tiles.ComponentDefinition; 25 import org.apache.struts.tiles.Controller; 26 import org.apache.struts.tiles.DefinitionsFactory; 27 import org.apache.struts.tiles.TilesUtilImpl; 28 29 import org.springframework.context.ApplicationContextException; 30 import org.springframework.web.servlet.view.InternalResourceView; 31 32 56 public class TilesView extends InternalResourceView { 57 58 64 public static final String PATH_ATTRIBUTE = TilesView.class.getName() + ".PATH"; 65 66 72 public static void setPath(HttpServletRequest request, String path) { 73 request.setAttribute(PATH_ATTRIBUTE, path); 74 } 75 76 77 private DefinitionsFactory definitionsFactory; 78 79 protected void initApplicationContext() throws ApplicationContextException { 80 super.initApplicationContext(); 81 82 this.definitionsFactory = 84 (DefinitionsFactory) getServletContext().getAttribute(TilesUtilImpl.DEFINITIONS_FACTORY); 85 if (this.definitionsFactory == null) { 86 throw new ApplicationContextException("Tiles definitions factory not found: TilesConfigurer not defined?"); 87 } 88 } 89 90 94 protected String prepareForRendering(HttpServletRequest request, HttpServletResponse response) 95 throws Exception { 96 97 ComponentDefinition definition = getComponentDefinition(this.definitionsFactory, request); 99 if (definition == null) { 100 throw new ServletException ("No Tiles definition found for name '" + getUrl() + "'"); 101 } 102 103 ComponentContext context = getComponentContext(definition, request); 105 106 Controller controller = getController(definition, request); 108 if (controller != null) { 109 if (logger.isDebugEnabled()) { 110 logger.debug("Executing Tiles controller [" + controller + "]"); 111 } 112 executeController(controller, context, request, response); 113 } 114 115 String path = getDispatcherPath(definition, request); 117 if (path == null) { 118 throw new ServletException ( 119 "Could not determine a path for Tiles definition '" + definition.getName() + "'"); 120 } 121 122 return path; 123 } 124 125 132 protected ComponentDefinition getComponentDefinition(DefinitionsFactory factory, HttpServletRequest request) 133 throws Exception { 134 return factory.getDefinition(getUrl(), request, getServletContext()); 135 } 136 137 144 protected ComponentContext getComponentContext(ComponentDefinition definition, HttpServletRequest request) 145 throws Exception { 146 ComponentContext context = ComponentContext.getContext(request); 147 if (context == null) { 148 context = new ComponentContext(definition.getAttributes()); 149 ComponentContext.setContext(context, request); 150 } 151 else { 152 context.addMissing(definition.getAttributes()); 153 } 154 return context; 155 } 156 157 165 protected Controller getController(ComponentDefinition definition, HttpServletRequest request) 166 throws Exception { 167 return definition.getOrCreateController(); 168 } 169 170 178 protected void executeController( 179 Controller controller, ComponentContext context, HttpServletRequest request, HttpServletResponse response) 180 throws Exception { 181 controller.perform(context, request, response, getServletContext()); 182 } 183 184 192 protected String getDispatcherPath(ComponentDefinition definition, HttpServletRequest request) 193 throws Exception { 194 Object pathAttr = request.getAttribute(PATH_ATTRIBUTE); 195 return (pathAttr != null ? pathAttr.toString() : definition.getPath()); 196 } 197 198 } 199 | Popular Tags |