1. opensymphony
  2. webwork

Commits

mbogaert  committed 0d8c34e

Fixed WW-676: support for TemplatePath init-param.

git-svn-id: http://svn.opensymphony.com/svn/webwork/trunk@611573baa09-0c28-0410-bef9-dab3c582ae83

  • Participants
  • Parent commits 1fce7dd
  • Branches master

Comments (0)

Files changed (1)

File src/java/com/opensymphony/webwork/views/freemarker/FreemarkerManager.java

View file
  • Ignore whitespace
 import com.opensymphony.xwork.Action;
 import com.opensymphony.xwork.ObjectFactory;
 import com.opensymphony.xwork.util.OgnlValueStack;
-import freemarker.cache.ClassTemplateLoader;
-import freemarker.cache.MultiTemplateLoader;
-import freemarker.cache.TemplateLoader;
-import freemarker.cache.WebappTemplateLoader;
+import freemarker.cache.*;
 import freemarker.ext.beans.BeansWrapper;
 import freemarker.ext.jsp.TaglibFactory;
 import freemarker.ext.servlet.HttpRequestHashModel;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 import javax.servlet.http.HttpSession;
+import java.io.File;
 import java.io.IOException;
 import java.io.InputStream;
 import java.util.Properties;
             try {
                 log.info("Instantiating Freemarker ConfigManager!, " + classname);
                 instance = (FreemarkerManager) ObjectFactory.getObjectFactory().buildBean(Class.forName(classname));
-            } catch (Exception e) {
+            }
+            catch (Exception e) {
                 log.fatal("Fatal exception occurred while trying to instantiate a Freemarker ConfigManager instance, " + classname, e);
             }
         }
             }
 
             model.put(KEY_SESSION_MODEL, sessionModel);
-        } else {
+        }
+        else {
             // no session means no attributes ???
             //            model.put(KEY_SESSION_MODEL, new SimpleHash());
         }
     }
 
     /**
-     * the default template loader is a MultiTemplateLoader which includes
-     * a ClassTemplateLoader and a WebappTemplateLoader
+     * The default template loader is a MultiTemplateLoader which includes
+     * a ClassTemplateLoader and a WebappTemplateLoader (and a FileTemplateLoader depending on
+     * the init-parameter 'TemplatePath').
      * <p/>
      * The ClassTemplateLoader will resolve fully qualified template includes
      * that begin with a slash. for example /com/company/template/common.ftl
      * The WebappTemplateLoader attempts to resolve templates relative to the web root folder
      */
     protected TemplateLoader getTemplateLoader(ServletContext servletContext) {
+        // construct a FileTemplateLoader for the init-param 'TemplatePath'
+        FileTemplateLoader templatePathLoader = null;
+
+        String templatePath = servletContext.getInitParameter("TemplatePath");
+        if (templatePath == null) templatePath = servletContext.getInitParameter("templatePath");
+        if (templatePath == null) templatePath = servletContext.getInitParameter("templatepath");
+
+        if (templatePath != null) {
+            try {
+                templatePathLoader = new FileTemplateLoader(new File(templatePath));
+            }
+            catch (IOException e) {
+                log.error("Invalid template path specified: " + e.getMessage(), e);
+            }
+        }
+
         // presume that most apps will require the class and webapp template loader
         // if people wish to
-        TemplateLoader multiLoader = new MultiTemplateLoader(new TemplateLoader[]{
-            new WebappTemplateLoader(servletContext),
-            new ClassTemplateLoader(FreemarkerResult.class, "/")
-        });
-
-        return multiLoader;
+        return templatePathLoader != null ?
+                new MultiTemplateLoader(new TemplateLoader[]{
+                    templatePathLoader,
+                    new WebappTemplateLoader(servletContext),
+                    new ClassTemplateLoader(FreemarkerResult.class, "/")
+                })
+                : new MultiTemplateLoader(new TemplateLoader[]{
+                    new WebappTemplateLoader(servletContext),
+                    new ClassTemplateLoader(FreemarkerResult.class, "/")
+                });
     }
 
     /**
-     * create the instance of the freemarker Configuration object
+     * Create the instance of the freemarker Configuration object.
      * <p/>
      * this implementation
      * <ul>
                 p.load(in);
                 configuration.setSettings(p);
             }
-        } catch (IOException e) {
+        }
+        catch (IOException e) {
             log.error("Error while loading freemarker settings from /freemarker.properties", e);
-        } catch (TemplateException e) {
+        }
+        catch (TemplateException e) {
             log.error("Error while loading freemarker settings from /freemarker.properties", e);
         }
     }