Index: WebWork.iml =================================================================== RCS file: /cvs/webwork/WebWork.iml,v retrieving revision 1.59 diff -u -r1.59 WebWork.iml --- WebWork.iml 8 Feb 2006 01:49:47 -0000 1.59 +++ WebWork.iml 16 Feb 2006 07:44:56 -0000 @@ -755,6 +755,24 @@ + + + + + + + + + + + + + + + + + + Index: lib/.cvsignore =================================================================== RCS file: /cvs/webwork/lib/.cvsignore,v retrieving revision 1.8 diff -u -r1.8 .cvsignore --- lib/.cvsignore 31 Dec 2005 08:15:34 -0000 1.8 +++ lib/.cvsignore 16 Feb 2006 07:44:57 -0000 @@ -20,3 +20,5 @@ cewolf hibernate pico +tiles +plexus \ No newline at end of file Index: webwork-default.xml =================================================================== RCS file: /src/java/webwork-default.xml,v retrieving revision 1.36 diff -u -r1.36 webwork-default.xml --- webwork-default.xml 7 Feb 2006 17:59:44 -0000 1.36 +++ webwork-default.xml 16 Feb 2006 07:46:42 -0000 @@ -12,6 +12,7 @@ + Index: src/java/com/opensymphony/webwork/views/tiles/TilesResult.java =================================================================== RCS file: src/java/com/opensymphony/webwork/views/tiles/TilesResult.java diff -N src/java/com/opensymphony/webwork/views/tiles/TilesResult.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ src/java/com/opensymphony/webwork/views/tiles/TilesResult.java 1 Jan 1970 00:00:00 -0000 @@ -0,0 +1,144 @@ +package com.opensymphony.webwork.views.tiles; + +import com.opensymphony.webwork.ServletActionContext; +import com.opensymphony.webwork.dispatcher.ServletDispatcherResult; +import com.opensymphony.xwork.ActionInvocation; +import com.opensymphony.xwork.LocaleProvider; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.apache.tiles.*; + +import javax.servlet.ServletContext; +import javax.servlet.ServletException; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.util.Locale; + +public class TilesResult extends ServletDispatcherResult { + private static final Log log = LogFactory.getLog(TilesResult.class); + + protected ActionInvocation invocation; + private DefinitionsFactory definitionsFactory; + + public void doExecute(String location, ActionInvocation invocation) throws Exception { + this.location = location; + this.invocation = invocation; + + HttpServletRequest request = ServletActionContext.getRequest(); + HttpServletResponse response = ServletActionContext.getResponse(); + ServletContext servletContext = ServletActionContext.getServletContext(); + + this.definitionsFactory = + (DefinitionsFactory) servletContext.getAttribute(TilesUtilImpl.DEFINITIONS_FACTORY); + + // get component definition + ComponentDefinition definition = getComponentDefinition(this.definitionsFactory, request); + if (definition == null) { + throw new ServletException("No Tiles definition found for name '" + location + "'"); + } + + // get current component context + ComponentContext context = getComponentContext(definition, request); + ComponentContext.setContext(context, request); + + // execute component controller associated with definition, if any + Controller controller = getController(definition, request); + if (controller != null) { + if (log.isDebugEnabled()) { + log.debug("Executing Tiles controller [" + controller + "]"); + } + executeController(controller, context, request, response); + } + + // determine the path of the definition + String path = getDispatcherPath(definition, request); + if (path == null) { + throw new ServletException( + "Could not determine a path for Tiles definition '" + definition.getName() + "'"); + } + + super.doExecute(path, invocation); + } + + protected Locale deduceLocale(HttpServletRequest request) { + if (invocation.getAction() instanceof LocaleProvider) { + return ((LocaleProvider) invocation.getAction()).getLocale(); + } else { + return request.getLocale(); + } + } + + /** + * Determine the Tiles component definition for the given Tiles + * definitions factory. + * @param factory the Tiles definitions factory + * @param request current HTTP request + * @return the component definition + */ + protected ComponentDefinition getComponentDefinition(DefinitionsFactory factory, HttpServletRequest request) + throws Exception { + ComponentDefinitions definitions = factory.readDefinitions(); + return definitions.getDefinition(location, deduceLocale(request)); + } + + /** + * Determine the Tiles component context for the given Tiles definition. + * @param definition the Tiles definition to render + * @param request current HTTP request + * @return the component context + * @throws Exception if preparations failed + */ + protected ComponentContext getComponentContext(ComponentDefinition definition, HttpServletRequest request) + throws Exception { + ComponentContext context = ComponentContext.getContext(request); + if (context == null) { + context = new ComponentContext(definition.getAttributes()); + ComponentContext.setContext(context, request); + } + else { + context.addMissing(definition.getAttributes()); + } + return context; + } + + /** + * Determine and initialize the Tiles component controller for the + * given Tiles definition, if any. + * @param definition the Tiles definition to render + * @param request current HTTP request + * @return the component controller to execute, or null if none + * @throws Exception if preparations failed + */ + protected Controller getController(ComponentDefinition definition, HttpServletRequest request) + throws Exception { + return definition.getOrCreateController(); + } + + /** + * Execute the given Tiles controller. + * @param controller the component controller to execute + * @param context the component context + * @param request current HTTP request + * @param response current HTTP response + * @throws Exception if controller execution failed + */ + protected void executeController( + Controller controller, ComponentContext context, HttpServletRequest request, HttpServletResponse response) + throws Exception { + controller.execute(context, request, response, ServletActionContext.getServletContext()); + } + + /** + * Determine the dispatcher path for the given Tiles definition, + * i.e. the request dispatcher path of the layout page. + * @param definition the Tiles definition to render + * @param request current HTTP request + * @return the path of the layout page to render + * @throws Exception if preparations failed + */ + protected String getDispatcherPath(ComponentDefinition definition, HttpServletRequest request) + throws Exception { + Object pathAttr = null; + return (pathAttr != null ? pathAttr.toString() : definition.getPath()); + } +}