struts-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From lukaszlen...@apache.org
Subject [07/30] struts git commit: Registers Tiles model
Date Mon, 18 Jan 2016 14:56:49 GMT
Registers Tiles model


Project: http://git-wip-us.apache.org/repos/asf/struts/repo
Commit: http://git-wip-us.apache.org/repos/asf/struts/commit/388861ff
Tree: http://git-wip-us.apache.org/repos/asf/struts/tree/388861ff
Diff: http://git-wip-us.apache.org/repos/asf/struts/diff/388861ff

Branch: refs/heads/master
Commit: 388861ff073f54f7959a35cc205b264237d043d0
Parents: 27ad6cb
Author: Lukasz Lenart <lukasz.lenart@gmail.com>
Authored: Fri Nov 27 13:36:52 2015 +0100
Committer: Lukasz Lenart <lukasz.lenart@gmail.com>
Committed: Fri Nov 27 13:36:52 2015 +0100

----------------------------------------------------------------------
 .../StrutsFreeMarkerAttributeRenderer.java      | 27 +++++++++++++++++++-
 1 file changed, 26 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/struts/blob/388861ff/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsFreeMarkerAttributeRenderer.java
----------------------------------------------------------------------
diff --git a/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsFreeMarkerAttributeRenderer.java
b/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsFreeMarkerAttributeRenderer.java
index 032f7e6..849053a 100644
--- a/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsFreeMarkerAttributeRenderer.java
+++ b/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsFreeMarkerAttributeRenderer.java
@@ -23,18 +23,25 @@ import com.opensymphony.xwork2.ActionContext;
 import com.opensymphony.xwork2.ActionInvocation;
 import com.opensymphony.xwork2.config.ConfigurationException;
 import com.opensymphony.xwork2.inject.Container;
+import freemarker.ext.beans.BeanModel;
+import freemarker.template.Configuration;
 import freemarker.template.TemplateException;
 import org.apache.logging.log4j.LogManager;
 import org.apache.logging.log4j.Logger;
 import org.apache.struts2.ServletActionContext;
+import org.apache.struts2.views.JspSupportServlet;
+import org.apache.struts2.views.freemarker.FreemarkerManager;
 import org.apache.struts2.views.freemarker.FreemarkerResult;
+import org.apache.struts2.views.freemarker.StrutsBeanWrapper;
 import org.apache.tiles.Attribute;
 import org.apache.tiles.context.TilesRequestContext;
+import org.apache.tiles.freemarker.template.TilesFMModelRepository;
 import org.apache.tiles.impl.InvalidTemplateException;
 import org.apache.tiles.renderer.impl.AbstractTypeDetectingAttributeRenderer;
 import org.apache.tiles.servlet.context.ServletTilesRequestContext;
 import org.apache.tiles.servlet.context.ServletUtil;
 
+import javax.servlet.ServletContext;
 import javax.servlet.http.HttpServletRequest;
 import java.io.IOException;
 
@@ -55,7 +62,8 @@ public class StrutsFreeMarkerAttributeRenderer extends AbstractTypeDetectingAttr
                 if (ctx == null) {
                     throw new ConfigurationException("There is no ActionContext for current
request!");
                 }
-                ActionInvocation invocation = ctx.getActionInvocation();
+
+                registerTilesBeanModel(ctx);
 
                 String include = (String) value;
                 FreemarkerResult result = new FreemarkerResult(include);
@@ -65,6 +73,7 @@ public class StrutsFreeMarkerAttributeRenderer extends AbstractTypeDetectingAttr
                 container.inject(result);
 
                 try {
+                    ActionInvocation invocation = ctx.getActionInvocation();
                     result.doExecute(include, invocation);
                 } catch (TemplateException e) {
                     LOG.error("Exception was thrown during rendering value {}: {}", value,
e.getMessage());
@@ -88,4 +97,20 @@ public class StrutsFreeMarkerAttributeRenderer extends AbstractTypeDetectingAttr
         return false;
     }
 
+    /**
+     * This register dedicated BeanModel to support tiles tags.
+     * It requires {@link org.apache.struts2.views.JspSupportServlet} to be registered in
web.xml
+     */
+    protected void registerTilesBeanModel(ActionContext ctx) {
+        ServletContext servletContext = ServletActionContext.getServletContext();
+        Configuration configuration = ctx.getInstance(FreemarkerManager.class).getConfiguration(servletContext);
+
+        StrutsBeanWrapper wrapper = (StrutsBeanWrapper) ctx.getInstance(FreemarkerManager.class).getWrapper();
+
+        LOG.trace("Adding support for Tiles tags, please remember to register {} in web.xml!",
JspSupportServlet.class.getName());
+
+        BeanModel tilesBeanModel = new BeanModel(new TilesFMModelRepository(), wrapper);
+        configuration.setSharedVariable("tiles", tilesBeanModel);
+    }
+
 }


Mime
View raw message