You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@struts.apache.org by dd...@apache.org on 2006/11/20 23:41:56 UTC

svn commit: r477379 - in /struts/struts2/trunk: apps/showcase/src/main/resources/ apps/showcase/src/main/webapp/WEB-INF/ apps/showcase/src/main/webapp/tiles/ core/src/main/java/org/apache/struts2/views/freemarker/ plugins/jsf/src/main/java/org/apache/s...

Author: ddewolf
Date: Mon Nov 20 14:41:56 2006
New Revision: 477379

URL: http://svn.apache.org/viewvc?view=rev&rev=477379
Log:
WW-1508  Supporting tiles/freemarker integration.

Added:
    struts/struts2/trunk/apps/showcase/src/main/webapp/tiles/body.ftl
    struts/struts2/trunk/plugins/tiles/src/main/java/org/apache/struts2/tiles/ConfiguredServletContext.java   (with props)
    struts/struts2/trunk/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsTilesListener.java   (with props)
Modified:
    struts/struts2/trunk/apps/showcase/src/main/resources/struts-tiles.xml
    struts/struts2/trunk/apps/showcase/src/main/webapp/WEB-INF/tiles.xml
    struts/struts2/trunk/apps/showcase/src/main/webapp/tiles/body.jsp
    struts/struts2/trunk/apps/showcase/src/main/webapp/tiles/layout.jsp
    struts/struts2/trunk/core/src/main/java/org/apache/struts2/views/freemarker/FreemarkerResult.java
    struts/struts2/trunk/plugins/jsf/src/main/java/org/apache/struts2/jsf/FacesResult.java
    struts/struts2/trunk/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsTilesContainerFactory.java
    struts/struts2/trunk/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsTilesRequestContext.java

Modified: struts/struts2/trunk/apps/showcase/src/main/resources/struts-tiles.xml
URL: http://svn.apache.org/viewvc/struts/struts2/trunk/apps/showcase/src/main/resources/struts-tiles.xml?view=diff&rev=477379&r1=477378&r2=477379
==============================================================================
--- struts/struts2/trunk/apps/showcase/src/main/resources/struts-tiles.xml (original)
+++ struts/struts2/trunk/apps/showcase/src/main/resources/struts-tiles.xml Mon Nov 20 14:41:56 2006
@@ -13,6 +13,10 @@
             <result name="success" type="tiles">showcase.index</result>
         </action>
 
+        <action name="freemarker">
+            <result type="tiles">showcase.freemarker</result>
+        </action>
+
         <action name="sanity">
             <result type="redirect">/tiles/layout.jsp</result>
             <result type="redirect" name="success">/tiles/layout.jsp</result>

Modified: struts/struts2/trunk/apps/showcase/src/main/webapp/WEB-INF/tiles.xml
URL: http://svn.apache.org/viewvc/struts/struts2/trunk/apps/showcase/src/main/webapp/WEB-INF/tiles.xml?view=diff&rev=477379&r1=477378&r2=477379
==============================================================================
--- struts/struts2/trunk/apps/showcase/src/main/webapp/WEB-INF/tiles.xml (original)
+++ struts/struts2/trunk/apps/showcase/src/main/webapp/WEB-INF/tiles.xml Mon Nov 20 14:41:56 2006
@@ -21,16 +21,22 @@
  */
 -->
 
- <!DOCTYPE tiles-definitions PUBLIC
-       "-//Apache Software Foundation//DTD Tiles Configuration 2.0//EN"
-       "http://struts.apache.org/dtds/tiles-config_2_0.dtd">
+<!DOCTYPE tiles-definitions PUBLIC
+        "-//Apache Software Foundation//DTD Tiles Configuration 2.0//EN"
+        "http://struts.apache.org/dtds/tiles-config_2_0.dtd">
 
 <tiles-definitions>
 
-  <definition name="showcase.index" template="/tiles/layout.jsp">
-	  <put name="title"  value="Tiles Showcase" />
-	  <put name="header" value="/tiles/header.jsp" />
-	  <put name="body"   value="/tiles/body.jsp" />
-  </definition>
+    <definition name="showcase.index" template="/tiles/layout.jsp">
+        <put name="title" value="Tiles Showcase"/>
+        <put name="header" value="/tiles/header.jsp"/>
+        <put name="body" value="/tiles/body.jsp"/>
+    </definition>
+
+    <definition name="showcase.freemarker" template="/tiles/layout.jsp">
+        <put name="title" value="Tiles/Freemarker Showcase"/>
+        <put name="header" value="/tiles/header.jsp"/>
+        <put name="body" value="/tiles/body.ftl"/>
+    </definition>
 
 </tiles-definitions>

Added: struts/struts2/trunk/apps/showcase/src/main/webapp/tiles/body.ftl
URL: http://svn.apache.org/viewvc/struts/struts2/trunk/apps/showcase/src/main/webapp/tiles/body.ftl?view=auto&rev=477379
==============================================================================
--- struts/struts2/trunk/apps/showcase/src/main/webapp/tiles/body.ftl (added)
+++ struts/struts2/trunk/apps/showcase/src/main/webapp/tiles/body.ftl Mon Nov 20 14:41:56 2006
@@ -0,0 +1,6 @@
+<div>
+    <p>This example illustrates the freemarker support in the Struts/Tiles Plugin.</p>
+
+    <p>Tiles 2 is an effort to extract the Tiles library from Struts. It is currently housed
+        in the Sandbox area of the Apache Struts Subversion repository.</p>
+</div>
\ No newline at end of file

Modified: struts/struts2/trunk/apps/showcase/src/main/webapp/tiles/body.jsp
URL: http://svn.apache.org/viewvc/struts/struts2/trunk/apps/showcase/src/main/webapp/tiles/body.jsp?view=diff&rev=477379&r1=477378&r2=477379
==============================================================================
--- struts/struts2/trunk/apps/showcase/src/main/webapp/tiles/body.jsp (original)
+++ struts/struts2/trunk/apps/showcase/src/main/webapp/tiles/body.jsp Mon Nov 20 14:41:56 2006
@@ -1,6 +1,15 @@
+<%@taglib prefix="s" uri="/struts-tags" %>
 <div>
     <p>This example illustrates the Struts/Tiles Plugin.</p>
 
     <p>Tiles 2 is an effort to extract the Tiles library from Struts. It is currently housed
         in the Sandbox area of the Apache Struts Subversion repository.</p>
+
+    <h4>Features</h4>
+    <ul>
+        <li>
+            <a href="freemarker.action">View FreeMarker Example</a>
+        </li>
+    </ul>
+
 </div>

Modified: struts/struts2/trunk/apps/showcase/src/main/webapp/tiles/layout.jsp
URL: http://svn.apache.org/viewvc/struts/struts2/trunk/apps/showcase/src/main/webapp/tiles/layout.jsp?view=diff&rev=477379&r1=477378&r2=477379
==============================================================================
--- struts/struts2/trunk/apps/showcase/src/main/webapp/tiles/layout.jsp (original)
+++ struts/struts2/trunk/apps/showcase/src/main/webapp/tiles/layout.jsp Mon Nov 20 14:41:56 2006
@@ -13,4 +13,3 @@
     </p>
 </body>
 </html>
-

Modified: struts/struts2/trunk/core/src/main/java/org/apache/struts2/views/freemarker/FreemarkerResult.java
URL: http://svn.apache.org/viewvc/struts/struts2/trunk/core/src/main/java/org/apache/struts2/views/freemarker/FreemarkerResult.java?view=diff&rev=477379&r1=477378&r2=477379
==============================================================================
--- struts/struts2/trunk/core/src/main/java/org/apache/struts2/views/freemarker/FreemarkerResult.java (original)
+++ struts/struts2/trunk/core/src/main/java/org/apache/struts2/views/freemarker/FreemarkerResult.java Mon Nov 20 14:41:56 2006
@@ -101,6 +101,7 @@
     protected Configuration configuration;
     protected ObjectWrapper wrapper;
     protected FreemarkerManager freemarkerManager;
+    private Writer writer;
 
     /*
      * Struts results are constructed for each result execution
@@ -199,10 +200,18 @@
         return configuration.getObjectWrapper();
     }
 
+
+    public void setWriter(Writer writer) {
+        this.writer = writer;
+    }
+
     /**
      * The default writer writes directly to the response writer.
      */
     protected Writer getWriter() throws IOException {
+        if(writer != null) {
+            return writer;
+        }
         return ServletActionContext.getResponse().getWriter();
     }
 

Modified: struts/struts2/trunk/plugins/jsf/src/main/java/org/apache/struts2/jsf/FacesResult.java
URL: http://svn.apache.org/viewvc/struts/struts2/trunk/plugins/jsf/src/main/java/org/apache/struts2/jsf/FacesResult.java?view=diff&rev=477379&r1=477378&r2=477379
==============================================================================
--- struts/struts2/trunk/plugins/jsf/src/main/java/org/apache/struts2/jsf/FacesResult.java (original)
+++ struts/struts2/trunk/plugins/jsf/src/main/java/org/apache/struts2/jsf/FacesResult.java Mon Nov 20 14:41:56 2006
@@ -48,8 +48,7 @@
      * config and then renders the result by delegating to the
      * FacesRender.render().
      *
-     * @see org.apache.struts2.dispatcher.StrutsResultSupport#doExecute(java.lang.String,
-     *      com.opensymphony.
+     * @see org.apache.struts2.dispatcher.StrutsResultSupport#doExecute(String, ActionInvocation)
      */
     protected void doExecute(String finalLocation, ActionInvocation invocation)
             throws Exception {

Added: struts/struts2/trunk/plugins/tiles/src/main/java/org/apache/struts2/tiles/ConfiguredServletContext.java
URL: http://svn.apache.org/viewvc/struts/struts2/trunk/plugins/tiles/src/main/java/org/apache/struts2/tiles/ConfiguredServletContext.java?view=auto&rev=477379
==============================================================================
--- struts/struts2/trunk/plugins/tiles/src/main/java/org/apache/struts2/tiles/ConfiguredServletContext.java (added)
+++ struts/struts2/trunk/plugins/tiles/src/main/java/org/apache/struts2/tiles/ConfiguredServletContext.java Mon Nov 20 14:41:56 2006
@@ -0,0 +1,185 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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 javax.servlet.RequestDispatcher;
+import javax.servlet.Servlet;
+import javax.servlet.ServletContext;
+import javax.servlet.ServletException;
+import java.io.InputStream;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.util.*;
+
+/**
+ * ServletContext implementation which allows Struts
+ * to inject initialization parameters into the context
+ * in order to reduce the amount of configuration required
+ * within web.xml for using Tiles.
+ *
+ * The specified init parameters are only utilized if
+ * they are not explicitaly defined in the web.xml
+ *
+ * @version $Rev$
+ * @since Struts 2.0.1
+ */
+@SuppressWarnings("deprecation")
+public class ConfiguredServletContext implements ServletContext {
+
+    private ServletContext rootContext;
+    private Map<String, String> initParameters;
+
+
+    public ConfiguredServletContext(ServletContext context, Map<String, String> initParameters) {
+        this.rootContext = context;
+        this.initParameters = initParameters;
+    }
+
+    public ServletContext getContext(String string) {
+        return rootContext.getContext(string);
+    }
+
+    public int getMajorVersion() {
+        return rootContext.getMajorVersion();
+    }
+
+    public int getMinorVersion() {
+        return rootContext.getMinorVersion();
+    }
+
+    public String getMimeType(String string) {
+        return rootContext.getMimeType(string);
+    }
+
+    public Set getResourcePaths(String string) {
+        return rootContext.getResourcePaths(string);
+    }
+
+    public URL getResource(String string) throws MalformedURLException {
+        return rootContext.getResource(string);
+    }
+
+    public InputStream getResourceAsStream(String string) {
+        return rootContext.getResourceAsStream(string);
+    }
+
+    public RequestDispatcher getRequestDispatcher(String string) {
+        return rootContext.getRequestDispatcher(string);
+    }
+
+    public RequestDispatcher getNamedDispatcher(String string) {
+        return rootContext.getNamedDispatcher(string);
+    }
+
+    @SuppressWarnings("deprecation")
+    public Servlet getServlet(String string) throws ServletException {
+        return rootContext.getServlet(string);
+    }
+
+    @SuppressWarnings("deprecation")
+    public Enumeration getServlets() {
+        return rootContext.getServlets();  //To change body of implemented methods use File | Settings | File Templates.
+    }
+
+    @SuppressWarnings("deprecation")
+    public Enumeration getServletNames() {
+        return rootContext.getServletNames();
+    }
+
+    public void log(String string) {
+        rootContext.log(string);
+    }
+
+    @SuppressWarnings("deprecation")
+    public void log(Exception exception, String string) {
+        rootContext.log(exception, string);
+    }
+
+    public void log(String string, Throwable throwable) {
+        rootContext.log(string, throwable);
+    }
+
+    public String getRealPath(String string) {
+        return rootContext.getRealPath(string);
+    }
+
+    public String getServerInfo() {
+        return rootContext.getServerInfo();
+    }
+
+    public String getInitParameter(String string) {
+        String parm = rootContext.getInitParameter(string);
+        if (parm == null) {
+            return initParameters.get(string);
+        }
+        return parm;
+    }
+
+    public Enumeration getInitParameterNames() {
+        return new CompositeEnumeration(
+                rootContext.getInitParameterNames(),
+                initParameters.keySet().iterator());
+    }
+
+    public Object getAttribute(String string) {
+        return rootContext.getAttribute(string);
+    }
+
+    public Enumeration getAttributeNames() {
+        return rootContext.getAttributeNames();
+    }
+
+    public void setAttribute(String string, Object object) {
+        rootContext.setAttribute(string, object);
+    }
+
+    public void removeAttribute(String string) {
+        rootContext.removeAttribute(string);
+    }
+
+    public String getServletContextName() {
+        return rootContext.getServletContextName();
+    }
+
+    class CompositeEnumeration implements Enumeration {
+
+        private Enumeration first;
+        private Iterator second;
+
+
+        public CompositeEnumeration(Enumeration first, Iterator second) {
+            this.first = first;
+            this.second = second;
+        }
+
+        public boolean hasMoreElements() {
+            return first.hasMoreElements() || second.hasNext();
+        }
+
+        public Object nextElement() {
+            if (first.hasMoreElements()) {
+                return first.nextElement();
+            }
+
+            return second.next();
+        }
+    }
+}

Propchange: struts/struts2/trunk/plugins/tiles/src/main/java/org/apache/struts2/tiles/ConfiguredServletContext.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: struts/struts2/trunk/plugins/tiles/src/main/java/org/apache/struts2/tiles/ConfiguredServletContext.java
------------------------------------------------------------------------------
    svn:keywords = Id Author Date Rev

Modified: struts/struts2/trunk/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsTilesContainerFactory.java
URL: http://svn.apache.org/viewvc/struts/struts2/trunk/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsTilesContainerFactory.java?view=diff&rev=477379&r1=477378&r2=477379
==============================================================================
--- struts/struts2/trunk/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsTilesContainerFactory.java (original)
+++ struts/struts2/trunk/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsTilesContainerFactory.java Mon Nov 20 14:41:56 2006
@@ -73,7 +73,7 @@
     }
 
     /**
-     * Wrapper factory, used to wrap the TilesRequestContext with a
+     * Wrapper factory, used to decorate the TilesRequestContext with a
      * FreemarkerResult aware version.
      * 
      */

Added: struts/struts2/trunk/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsTilesListener.java
URL: http://svn.apache.org/viewvc/struts/struts2/trunk/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsTilesListener.java?view=auto&rev=477379
==============================================================================
--- struts/struts2/trunk/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsTilesListener.java (added)
+++ struts/struts2/trunk/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsTilesListener.java Mon Nov 20 14:41:56 2006
@@ -0,0 +1,73 @@
+/*
+ * $Id$
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.TilesContainer;
+import org.apache.tiles.TilesException;
+import org.apache.tiles.factory.TilesContainerFactory;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+import javax.servlet.ServletContext;
+import java.util.Map;
+import java.util.HashMap;
+
+/**
+ * Listener used to automatically inject ServletContext
+ * init parameters so that they don't need to be configured
+ * explicitly for tiles integration.  This is provided
+ * mainly for backwards compatibility with Struts 2.0.1
+ * configuration.
+ *
+ * @since Struts 2.0.2
+ * @version $Rev$
+ *
+ */
+public class StrutsTilesListener extends TilesListener {
+
+    private static final Log LOG =
+        LogFactory.getLog(StrutsTilesListener.class);
+
+    private static final Map<String, String> INIT;
+
+    static {
+        INIT = new HashMap<String, String>();
+        INIT.put(TilesContainerFactory.CONTAINER_FACTORY_INIT_PARAM,
+                 StrutsTilesContainerFactory.class.getName());
+    }
+
+    protected TilesContainer createContainer(ServletContext context)
+    throws TilesException {
+        if(context.getInitParameter(TilesContainerFactory.CONTEXT_FACTORY_INIT_PARAM) == null) {
+            context = decorate(context);
+        }
+        else {
+            LOG.warn("Tiles container factory is explicitly set.  Not injecting struts configuration.");
+        }
+        return super.createContainer(context);
+    }
+
+    protected ServletContext decorate(ServletContext context) {
+        return new ConfiguredServletContext(context, INIT);
+    }
+
+}

Propchange: struts/struts2/trunk/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsTilesListener.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: struts/struts2/trunk/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsTilesListener.java
------------------------------------------------------------------------------
    svn:keywords = Id Author Date Rev

Modified: struts/struts2/trunk/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsTilesRequestContext.java
URL: http://svn.apache.org/viewvc/struts/struts2/trunk/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsTilesRequestContext.java?view=diff&rev=477379&r1=477378&r2=477379
==============================================================================
--- struts/struts2/trunk/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsTilesRequestContext.java (original)
+++ struts/struts2/trunk/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsTilesRequestContext.java Mon Nov 20 14:41:56 2006
@@ -21,16 +21,18 @@
 package org.apache.struts2.tiles;
 
 import com.opensymphony.xwork2.ActionInvocation;
-import freemarker.template.TemplateException;
+import com.opensymphony.xwork2.inject.Container;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.apache.struts2.ServletActionContext;
+import org.apache.struts2.dispatcher.Dispatcher;
 import org.apache.struts2.views.freemarker.FreemarkerResult;
 import org.apache.tiles.context.TilesRequestContext;
 import org.apache.tiles.context.TilesRequestContextWrapper;
 
 import javax.servlet.ServletException;
 import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
 import java.io.IOException;
 
 /**
@@ -49,6 +51,7 @@
     private static final Log LOG =
             LogFactory.getLog(StrutsTilesRequestContext.class);
 
+
     /**
      * The mask used to detect requests which should be intercepted.
      */
@@ -86,8 +89,6 @@
      * be used in order to setup the appropriate model.
      *
      * @throws IOException
-     * @throws ServletException
-     * @throws Exception
      */
     public void include(String include) throws IOException {
         if (include.endsWith(mask)) {
@@ -95,20 +96,29 @@
                 LOG.debug("Intercepting tiles include '" + include + "'. Processing as freemarker result.");
             }
             HttpServletRequest request = (HttpServletRequest) getRequest();
+            HttpServletResponse response = (HttpServletResponse) getResponse();
 
             ActionInvocation invocation =
                     ServletActionContext.getActionContext(request).getActionInvocation();
 
-            FreemarkerResult result = new FreemarkerResult();
-
             try {
+                FreemarkerResult result = new FreemarkerResult();
+                result.setWriter(response.getWriter());
+
+                Container container = Dispatcher.getInstance()
+                        .getConfigurationManager()
+                        .getConfiguration().getContainer();
+
+                container.inject(result);
+
                 result.doExecute(include, invocation);
-            } catch (TemplateException e) {
+            } catch (Exception e) {
                 LOG.error("Error invoking Freemarker template", e);
-                throw new IOException("Error invoking Freemarker template."+ e.getMessage());
+                throw new IOException("Error invoking Freemarker template." + e.getMessage());
             }
         } else {
             super.include(include);
         }
     }
+
 }