velocity-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Peter Locke" <plo...@abebooks.com>
Subject RE: Trouble Loading a Template from a WAR file - Velocity 1.4 - WLS 8.12
Date Tue, 15 Jun 2004 16:22:10 GMT
When using a class path resource loader in a servlet environment
(recommended) a problem can be that a different classloader is used to load
the .jar containing the templates than is used to actually load the class
resource loader from velocity, meaning it can never find any of your
templates.

I use a simple subclass of the velocity classpath resource loader, and as
long as you jar it up with your templates you can put the .jar anywhere in
the classpath, and the templates will always be found using this loader and
their fully qualified classpath name.

Peter

-----snip----------

public class ABEVelocityTemplateLoader extends ClasspathResourceLoader {
  public ABEVelocityTemplateLoader() {
  }

  public synchronized InputStream getResourceStream(String name)
        throws ResourceNotFoundException
    {
        InputStream result = null;
        if(name == null || name.length() == 0)
            throw new ResourceNotFoundException("No template name provided");
        try
        {
            ClassLoader classLoader = getClass().getClassLoader();
            result = classLoader.getResourceAsStream(name);

            if (result == null)
            {
              result = ClassLoader.getSystemResourceAsStream(name);
            }
        }
        catch(Exception fnfe)
        {
            throw new ResourceNotFoundException(fnfe.getMessage());
        }
        return result;
    }


}

----------------------------
-----Original Message-----
From: Brian Utt [mailto:brian_utt@yahoo.com] 
Sent: Tuesday, June 15, 2004 7:42 AM
To: Velocity Users List
Subject: RE: Trouble Loading a Template from a WAR file - Velocity 1.4 - WLS
8.12

Arindam,

I tried each of these (incorporated my application into an EAR file and
placing the template in the WEB-INF/classes directory and using a jar file to
store the templates in the WEB-INF/lib directory) and both complain with the
same message.  I would prefer to still use the WAR file, and thus the
WEB-INF/lib solution would be a nice one to use.  How do you reference the
template using that solution, would I lookup "PoolXML.vm" or "/PoolXML.vm" or
should both work?  

Thanks!
-brian


--- "Gupta, Arindam" <arindam_gupta@fanniemae.com>
wrote:
> I have had success with the following solutions:
> 
> 1. Deploy the template files outside the ear file (in my case the 
> deployment unit is an ear file) in the WEB-INF/classes or 2. jar up 
> the templates and place the jar file in the WEB-INF/lib
> 
> Hope this helps.
> 
> -AG
> 
> -----Original Message-----
> From: Brian Utt [mailto:brian_utt@yahoo.com]
> Sent: Tuesday, June 15, 2004 10:04 AM
> To: velocity-user@jakarta.apache.org
> Subject: Trouble Loading a Template from a WAR file
> - Velocity 1.4 - WLS
> 8.12
> 
> 
> I am having trouble using the Velocity 1.4 package and loading a 
> template from a WAR file.  I can successfully use the package with a 
> command line java application, but when I ported it into a servlet, I 
> get the infamous "Unable to find resource..." message.
> 
> I have tried to load the template with a generic servlet as well as 
> extending the VelocityServlet and I seem to be missing an important 
> step.  My .vm Template files are in my deployed WAR file in the 
> WEB-INF/classes "folder" and I am running on Weblogic 8.12.
> 
> Below is my servlet code:
> 
> _____________________________________
> 
> public class DataDownloadServlet 
>        extends VelocityServlet
> {
>   public Template handleRequest( 
>          HttpServletRequest request, 
>          HttpServletResponse response, 
>          Context context ) 
>   throws IOException, ServletException 
>   {
>     try
>     {
>       //get the pool number to extract the data for.
>       String[] reqVals = 
>         request.getParameterValues("pn");
> 
>       String uri = request.getRequestURI();
> 
>       List beans = 
>         InternetDataManager.process(reqVals);
> 
>       Template template = null;
>       String templateName = "";
> 
>      
> response.setContentType("application/vnd.xml");
>       templateName = "PoolXML.vm";
> 
>       try
>       {
>           template = getTemplate(templateName);
> 
>           context.put("pools", beans);
> 
>           mergeTemplate(template, context, response);
>       }
>       catch( ResourceNotFoundException rnfe )
>       {
>         // couldn't find the template
>         System.out.println("Template Not Found: " + 
>                             templateName + 
>                             ": " +
> rnfe.getMessage());
>         throw new ServletException(rnfe);
>       }
>       catch( ParseErrorException pee )
>       {
>         // syntax error : problem parsing the template
>         System.out.println("Prob Parsing template "+
>                             templateName + 
>                             ": " +pee.getMessage());
>         throw new ServletException(pee);
>       }
>       catch( Exception e )
>       {
>         System.out.println("Weird Problem: " + 
>                             e.getMessage());
>         throw new ServletException(e);
>       }
>     }
>     catch (Exception ex)
>     {
>       System.out.println("Caughted exception: " + 
>                           ex.getMessage());
>       ex.printStackTrace(System.out);
> 
>       throw new ServletException(ex);
>     }
>     return null;
>   }
> }
> 
> _____________________________________
> 
> 
> I have read about a velocity.properties file, but I have not used one 
> on my command line app and I was hoping not to have to use one on the 
> j2ee app, but I might very well be wrong about that.  Any assistance 
> will be very much appreciated!
> 
> Thanks in advance!
> 
> -Brian
> 
> 
> 
> =====
> Debugging is twice as hard as writing the code in the first place.
> Therefore, if you write the code as cleverly as possible, you are, by 
> definition, not smart enough to debug it.
> --Kernighan
> 
>
---------------------------------------------------------------------
> To unsubscribe, e-mail:
> velocity-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail:
> velocity-user-help@jakarta.apache.org
> 
> 
>
---------------------------------------------------------------------
> To unsubscribe, e-mail:
> velocity-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail:
> velocity-user-help@jakarta.apache.org
> 
> 


=====
Debugging is twice as hard as writing the code in the first place. Therefore,
if you write the code as cleverly as possible, you are, by definition, not
smart enough to debug it. --Kernighan

---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org


Mime
View raw message