Author: mrdon Date: Thu Sep 28 22:53:44 2006 New Revision: 451137 URL: http://svn.apache.org/viewvc?view=rev&rev=451137 Log: Updated tiles dependency to 2.0-snapshot, added support for tiles and freemarker integration WW-1418 WW-1450
Added: struts/struts2/trunk/core/src/main/java/org/apache/struts2/tiles/ struts/struts2/trunk/core/src/main/java/org/apache/struts2/tiles/StrutsTilesListener.java struts/struts2/trunk/core/src/main/java/org/apache/struts2/tiles/StrutsTilesUtilImpl.java Modified: struts/struts2/trunk/core/pom.xml struts/struts2/trunk/core/src/main/java/org/apache/struts2/views/tiles/TilesResult.java Modified: struts/struts2/trunk/core/pom.xml URL: http://svn.apache.org/viewvc/struts/struts2/trunk/core/pom.xml?view=diff&rev=451137&r1=451136&r2=451137 ============================================================================== --- struts/struts2/trunk/core/pom.xml (original) +++ struts/struts2/trunk/core/pom.xml Thu Sep 28 22:53:44 2006 @@ -204,7 +204,7 @@ <dependency> <groupId>org.apache.struts.tiles</groupId> <artifactId>tiles-core</artifactId> - <version>0.2-SNAPSHOT</version> + <version>2.0-SNAPSHOT</version> <optional>true</optional> </dependency> <dependency> Added: struts/struts2/trunk/core/src/main/java/org/apache/struts2/tiles/StrutsTilesListener.java URL: http://svn.apache.org/viewvc/struts/struts2/trunk/core/src/main/java/org/apache/struts2/tiles/StrutsTilesListener.java?view=auto&rev=451137 ============================================================================== --- struts/struts2/trunk/core/src/main/java/org/apache/struts2/tiles/StrutsTilesListener.java (added) +++ struts/struts2/trunk/core/src/main/java/org/apache/struts2/tiles/StrutsTilesListener.java Thu Sep 28 22:53:44 2006 @@ -0,0 +1,61 @@ +/* + * $Id: StrutsSpringObjectFactory.java 439747 2006-09-03 09:22:46Z mrdon $ + * + * Copyright 2006 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.struts2.tiles; + +import org.apache.tiles.listener.TilesListener; +import org.apache.tiles.TilesUtilImpl; +import org.apache.tiles.TilesUtil; + +import javax.servlet.ServletContextEvent; +import javax.servlet.ServletContext; + +/** + * Custom TilesListener which can be used to allow freemarker + * invocation from tiles components. + * + * @version $Rev: 397992 $ $Date$ + */ +public class StrutsTilesListener extends TilesListener { + + /** + * The key used to identify the freemarker mask. + */ + public static final String MASK_INIT_PARAM = "freemarker-mask"; + + /** + * Configured mask; + */ + private String mask; + + /** + * Initialize the tiles system, overriding the TilesUtilImpl + * @param servletContextEvent + */ + public void contextInitialized(ServletContextEvent servletContextEvent) { + ServletContext context = servletContextEvent.getServletContext(); + mask = context.getInitParameter(MASK_INIT_PARAM); + + if(mask == null) { + mask = ".ftl"; + } + + TilesUtilImpl tilesUtil = new StrutsTilesUtilImpl(); + TilesUtil.setTilesUtil(tilesUtil); + super.contextInitialized(servletContextEvent); + } +} Added: struts/struts2/trunk/core/src/main/java/org/apache/struts2/tiles/StrutsTilesUtilImpl.java URL: http://svn.apache.org/viewvc/struts/struts2/trunk/core/src/main/java/org/apache/struts2/tiles/StrutsTilesUtilImpl.java?view=auto&rev=451137 ============================================================================== --- struts/struts2/trunk/core/src/main/java/org/apache/struts2/tiles/StrutsTilesUtilImpl.java (added) +++ struts/struts2/trunk/core/src/main/java/org/apache/struts2/tiles/StrutsTilesUtilImpl.java Thu Sep 28 22:53:44 2006 @@ -0,0 +1,96 @@ +/* + * $Id: StrutsSpringObjectFactory.java 439747 2006-09-03 09:22:46Z mrdon $ + * + * Copyright 2006 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.struts2.tiles; + +import org.apache.tiles.TilesUtilImpl; +import org.apache.struts2.views.freemarker.FreemarkerResult; +import org.apache.struts2.ServletActionContext; + +import javax.servlet.jsp.PageContext; +import javax.servlet.ServletException; +import javax.servlet.http.HttpServletRequest; +import java.io.IOException; + +import com.opensymphony.xwork2.ActionInvocation; +import freemarker.template.TemplateException; + +/** + * + * Default implementation of TilesUtil. + * This class contains default implementation of utilities. This implementation + * is intended to be used without Struts. + * + * TilesUtilImpl implementation used to intercept .ftl requests and + * ensure that they are setup properly to take advantage of the + * [EMAIL PROTECTED] FreemarkerResult}. + * + * @version $Id$ + * + */ +public class StrutsTilesUtilImpl extends TilesUtilImpl { + + /** + * The mask used to detect requests which should be intercepted. + */ + private String mask; + + /** + * Default constructor. + * Sets the mask to '.ftl' + */ + public StrutsTilesUtilImpl() { + mask = ".ftl"; + } + + /** + * Optional constructor used to specify a specific mask. + * @param mask + */ + public StrutsTilesUtilImpl(String mask) { + this.mask = mask; + } + + /** + * Enhancement of the default include which allows for freemarker + * templates to be intercepted so that the FreemarkerResult can + * be used in order to setup the appropriate model. + * + * @param string the included resource + * @param pageContext the current page context + * @param b whether or not a flush should occur + * @throws IOException + * @throws ServletException + * @throws Exception + */ + public void doInclude(String string, PageContext pageContext, boolean b) throws Exception { + if(string.endsWith(".ftl")) { + HttpServletRequest request = (HttpServletRequest) pageContext.getRequest(); + ActionInvocation invocation = ServletActionContext.getActionContext(request).getActionInvocation(); + FreemarkerResult result = new FreemarkerResult(); + try { + result.doExecute(string, invocation); + } catch (TemplateException e) { + log.error("Error invoking Freemarker template", e); + throw new ServletException("Error invoking Freemarker template.", e); + } + } + else { + super.doInclude(string, pageContext, b); + } + } +} Modified: struts/struts2/trunk/core/src/main/java/org/apache/struts2/views/tiles/TilesResult.java URL: http://svn.apache.org/viewvc/struts/struts2/trunk/core/src/main/java/org/apache/struts2/views/tiles/TilesResult.java?view=diff&rev=451137&r1=451136&r2=451137 ============================================================================== --- struts/struts2/trunk/core/src/main/java/org/apache/struts2/views/tiles/TilesResult.java (original) +++ struts/struts2/trunk/core/src/main/java/org/apache/struts2/views/tiles/TilesResult.java Thu Sep 28 22:53:44 2006 @@ -28,12 +28,8 @@ import org.apache.commons.logging.LogFactory; import org.apache.struts2.ServletActionContext; import org.apache.struts2.dispatcher.ServletDispatcherResult; -import org.apache.tiles.ComponentContext; -import org.apache.tiles.ComponentDefinition; -import org.apache.tiles.ComponentDefinitions; -import org.apache.tiles.Controller; -import org.apache.tiles.DefinitionsFactory; -import org.apache.tiles.TilesUtilImpl; +import org.apache.tiles.*; +import org.apache.tiles.context.servlet.ServletTilesContext; import com.opensymphony.xwork2.ActionInvocation; import com.opensymphony.xwork2.LocaleProvider; @@ -104,6 +100,7 @@ HttpServletRequest request = ServletActionContext.getRequest(); HttpServletResponse response = ServletActionContext.getResponse(); ServletContext servletContext = ServletActionContext.getServletContext(); + TilesContext tilesContext = new ServletTilesContext(servletContext, request, response); this.definitionsFactory = (DefinitionsFactory) servletContext.getAttribute(TilesUtilImpl.DEFINITIONS_FACTORY); @@ -115,8 +112,8 @@ } // get current component context - ComponentContext context = getComponentContext(definition, request); - ComponentContext.setContext(context, request); + ComponentContext context = getComponentContext(definition, tilesContext); + ComponentContext.setContext(context, tilesContext); // execute component controller associated with definition, if any Controller controller = getController(definition, request); @@ -124,7 +121,7 @@ if (log.isDebugEnabled()) { log.debug("Executing Tiles controller [" + controller + "]"); } - executeController(controller, context, request, response); + executeController(controller, context, tilesContext); } // determine the path of the definition @@ -163,16 +160,16 @@ * Determine the Tiles component context for the given Tiles definition. * * @param definition the Tiles definition to render - * @param request current HTTP request + * @param tilesContext current TilesContext * @return the component context * @throws Exception if preparations failed */ - protected ComponentContext getComponentContext(ComponentDefinition definition, HttpServletRequest request) + protected ComponentContext getComponentContext(ComponentDefinition definition, TilesContext tilesContext) throws Exception { - ComponentContext context = ComponentContext.getContext(request); + ComponentContext context = ComponentContext.getContext(tilesContext); if (context == null) { context = new ComponentContext(definition.getAttributes()); - ComponentContext.setContext(context, request); + ComponentContext.setContext(context, tilesContext); } else { context.addMissing(definition.getAttributes()); } @@ -198,14 +195,13 @@ * * @param controller the component controller to execute * @param context the component context - * @param request current HTTP request - * @param response current HTTP response + * @param tilesContext current tilesContext * @throws Exception if controller execution failed */ protected void executeController( - Controller controller, ComponentContext context, HttpServletRequest request, HttpServletResponse response) + Controller controller, ComponentContext context, TilesContext tilesContext) throws Exception { - controller.execute(context, request, response, ServletActionContext.getServletContext()); + controller.execute(tilesContext, context); } /**