Here you go.

Dave Polito

-----Original Message-----
From: Jason van Zyl [mailto:[EMAIL PROTECTED]]
Sent: Monday, May 21, 2001 10:11 AM
To: Turbine Dev
Subject: JspService and multiple template paths


Hi,

I think someone posted a patch for the JSP service that allowed multiple
templates paths. I don't think we integrated it, whoever submitted the
patch could you please do so again as I would  like to get it in
if I can. Sorry this one slipped through. I don't remember it
going multiple template paths don't appear to be working in
the JspService.

I am also integrating a little JSP example into the TDK that
was sent to me a while back. Sorry for the delay.

Thanks,

-- 
jvz.

Jason van Zyl
[EMAIL PROTECTED]

http://jakarta.apache.org/velocity
http://jakarta.apache.org/turbine
http://jakarta.apache.org/commons
http://tambora.zenplex.org

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

-------------------------------------------
[PATCH]

Fixed a problem in the handleRequest method, where getRequestDispatcher was
being called with paths[0] + filename.  The paths[] was populated with
absolute path names,  but servlet > 2.1 seems to need a relative pathname.
So I changed String[] paths to String[] templatePaths. The array
templatePaths is still needed for templateExists.  I also added an array for
relativeTemplatePaths, and a method that will return the
relativeTemplateName if it exists.  This will allow multiple template.paths
to be used for jsp.

Dave Polito

Index: TurbineJspService.java
===================================================================
RCS file:
/home/cvspublic/jakarta-turbine/src/java/org/apache/turbine/services/jsp/Tur
bineJspService.java,v
retrieving revision 1.13
diff -u -r1.13 TurbineJspService.java
--- TurbineJspService.java      2001/04/02 05:49:36     1.13
+++ TurbineJspService.java      2001/04/10 19:41:09
@@ -88,8 +88,11 @@
 public class TurbineJspService extends BaseTemplateEngineService
     implements JspService
 {
-    /** The base path prepended to filenames given in arguments */
-    private String[] paths;
+    /** The base path[s] prepended to filenames given in arguments */
+    private String[] templatePaths;
+    
+    /** The relative path[s] prepended to filenames */
+    private String[] relativeTemplatePaths;
 
     /** The buffer size for the output stream. */
     private int bufferSize;
@@ -165,9 +168,18 @@
     public void handleRequest(RunData data, String filename, boolean
isForward) 
         throws TurbineException
     {                                
+        /** template name with relative path */
+        String relativeTemplateName = getRelativeTemplateName(filename);
+        
+        if (relativeTemplateName == null)
+        {
+            throw new TurbineException(
+            "Template " + filename + " not found in template paths");
+        }
+        
         // get the RequestDispatcher for the JSP
         RequestDispatcher dispatcher = data.getServletContext()
-            .getRequestDispatcher(paths[0] + filename);
+        .getRequestDispatcher(relativeTemplateName);
         
         try
         {
@@ -215,19 +227,25 @@
          * Use the turbine template service to translate
          * the template paths.
          */
-        paths = TurbineTemplate.translateTemplatePaths(
-            config.getStringArray("templates"));
+        templatePaths = TurbineTemplate.translateTemplatePaths(
+        config.getStringArray("templates"));
         
-        /* not working properly - removing until I can investigate (jdm)
-        if (path.startsWith("/"))
-        {
-            path = path.substring(1);
-        }
-        if (path.length() > 0 && !path.endsWith("/"))
+        /*
+         * Set relative paths from config.
+         * Needed for javax.servlet.RequestDispatcher
+         */
+        relativeTemplatePaths = config.getStringArray("templates");
+        
+        /*
+         * Make sure that the relative paths begin with /
+         */
+        for (int i = 0; i < relativeTemplatePaths.length; i++)
         {
-            path += "/";
+            if (!relativeTemplatePaths[i].startsWith("/"))
+            {
+                relativeTemplatePaths[i] = "/" + relativeTemplatePaths[i];
+            }
         }
-        */
         
         bufferSize = config.getInt("buffer.size", 8192);
     
@@ -247,7 +265,36 @@
      * @return boolean
      */
     public boolean templateExists(String template)
+    {
+        return TurbineTemplate.templateExists(template, templatePaths);
+    } 
+    /**
+     * Searchs for a template in the default.template path[s] and
+     * returns the template name with a relative path which is
+     * required by <a
href="http://java.sun.com/products/servlet/2.3/javadoc/javax/servlet/Servlet
Context.html#getRequestDispatcher(java.lang.String)">
+     * javax.servlet.RequestDispatcher</a>
+     *
+     * @param String template
+     * @return String
+     */
+
+    public String getRelativeTemplateName(String template)
     {
-        return TurbineTemplate.templateExists(template, paths);
-    }              
+        /* A dummy String[] object used to pass a String to
+           TurbineTemplate.templateExists
+         */
+        String[] testTemplatePath = new String[1];
+        
+        /** Find which template path the template is in */
+        for (int i = 0; i < relativeTemplatePaths.length; i++)
+        {
+            testTemplatePath[0] =
TurbineServlet.getRealPath(relativeTemplatePaths[i]);
+            if (TurbineTemplate.templateExists(template, testTemplatePath))
+            {
+                return relativeTemplatePaths[i] + template;
+            }
+        }
+        
+        return null;
+    }             
 }

Reply via email to