Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Velocity Templating question

Posted on 2002-06-26
7
Medium Priority
?
412 Views
Last Modified: 2013-11-24
Hello,

Im trying to get to know the velocity templating engine.  Ive installed velocity, and i have working examples.  However, im trying to configure my VelocityServlet (child of..) to use templates from my template directory, but its not doing this.. i have the relative path to my properties file in my web.xml , and my properties file in turn has the entry :

file.resource.loader.path = /template

The templates, (weirdly) are being found.. but not where i expect them to be - they are in a completely seperate web application.  Its almost as though there is a general properties file over-riding the new one which i secify in loadConfiguration ();

The path to the props file is being read from the web.xml ok, and the path to the template directory is also reported in loadConfiguration as being correct..

any help appreciated..


package com.mehdi.cms;


import java.io.IOException;
import java.io.FileNotFoundException;

import java.util.Properties;
import java.util.Vector;

import javax.servlet.ServletConfig;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.velocity.Template;
import org.apache.velocity.context.Context;
import org.apache.velocity.servlet.VelocityServlet;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.exception.ResourceNotFoundException;
import org.apache.velocity.exception.ParseErrorException;

import java.io.*;
import java.util.*;


public class Test extends VelocityServlet
{

    protected Properties loadConfiguration(ServletConfig config )
        throws IOException, FileNotFoundException
    {

        String propsFile = config.getInitParameter(INIT_PROPS_KEY);
        if ( propsFile != null )
        {
            String realPath = getServletContext().getRealPath(propsFile);

            if ( realPath != null )
            {
                propsFile = realPath;
            }
        }

       Properties p = new Properties();
       p.load( new FileInputStream(propsFile) );
       String path = p.getProperty("file.resource.loader.path");

       if (path != null)
       {
           path = getServletContext().getRealPath( path );
           p.setProperty( "file.resource.loader.path", path );
       }

       System.out.println(">>" + p.getProperty("file.resource.loader.path"));//THIS IS CORRECT !

       path = p.getProperty("runtime.log");

       if (path != null)
       {
           path = getServletContext().getRealPath( path );
           p.setProperty("runtime.log", path );
       }

       return p;
    }

    public Template handleRequest( HttpServletRequest request,
     HttpServletResponse response, Context ctx )
    {
        String p1 = "doo";
        String p2 = "daa";

        Vector personList = new Vector();
        personList.addElement( p1 );
        personList.addElement( p2 );

        ctx.put("theList", personList );

        Template outty = null;

        try
        {
            outty =  getTemplate("mehdi.vm");
        }
        catch( ParseErrorException pee )
        {
            System.out.println("SampleServlet : parse error for template " + pee);
        }
        catch( ResourceNotFoundException rnfe )
        {
            System.out.println("SampleServlet : template not found " + rnfe);
        }
        catch( Exception e )
        {
            System.out.println("Error " + e);
        }
        return outty;
    }
}
0
Comment
Question by:mehdi
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
7 Comments
 
LVL 92

Accepted Solution

by:
objects earned 200 total points
ID: 7112219
I'd recomend using the class loader to access your properties files instead of specifying location on disk.
Either by using ResourceBundle, ClassLoader.getSystemResourceAsStream, or Class.getResourceAsStream.
0
 
LVL 1

Author Comment

by:mehdi
ID: 7122174
Ok Objects,

Thanks for the suggestion, here is what i have now in my loadConfig.  I have put velocity.properties in my classpath, and it is being picked up fine.. still no joy though.  Still the templates are not being read from where i want them to be read.

Thanks,

Mehdi


    protected Properties loadConfiguration(ServletConfig config )
        throws IOException, FileNotFoundException
    {

       Properties p = new Properties();
       p.load(ClassLoader.getSystemResourceAsStream("velocity.properties"));
       System.out.print("WE ARE USING : " + p.getProperty(Velocity.FILE_RESOURCE_LOADER_PATH));
       return p;
    }

0
 
LVL 1

Author Comment

by:mehdi
ID: 7122183
.. needless to say of course that the following line is in my velocity.properties :

file.resource.loader.path=D:\\webcontainer\\mehdi_cms\\templates

regards,

Mehdi
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 92

Expert Comment

by:objects
ID: 7123344
What directory is the properties file that is being loaded in?
ANd what is the base directory of your webapp?
0
 
LVL 1

Author Comment

by:mehdi
ID: 7124122
velocity.properties is in :

d:\classes\velocity.properties

contains :
file.resource.loader.path=D:\\webcontainer\\mehdi_cms\\templates

and my webapp is in

D:\webcontainer\mehdi_cms

Cheers,

Mehdi



0
 
LVL 35

Expert Comment

by:girionis
ID: 8893353
No comment has been added lately, so it's time to clean up this TA.

I will leave a recommendation in the Cleanup topic area that this question is:

- To be deleted and points refunded

Please leave any comments here within the
next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER !

girionis
Cleanup Volunteer
0
 
LVL 92

Expert Comment

by:objects
ID: 8897976
0

Featured Post

Enroll in October's Free Course of the Month

Do you work with and analyze data? Enroll in October's Course of the Month for 7+ hours of SQL training, allowing you to quickly and efficiently store or retrieve data. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses

618 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question