mehdi
asked on
Velocity Templating question
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.FileNotFoundExcept ion;
import java.util.Properties;
import java.util.Vector;
import javax.servlet.ServletConfi g;
import javax.servlet.http.HttpSer vletReques t;
import javax.servlet.http.HttpSer vletRespon se;
import org.apache.velocity.Templa te;
import org.apache.velocity.contex t.Context;
import org.apache.velocity.servle t.Velocity Servlet;
import org.apache.velocity.app.Ve locity;
import org.apache.velocity.except ion.Resour ceNotFound Exception;
import org.apache.velocity.except ion.ParseE rrorExcept ion;
import java.io.*;
import java.util.*;
public class Test extends VelocityServlet
{
protected Properties loadConfiguration(ServletC onfig config )
throws IOException, FileNotFoundException
{
String propsFile = config.getInitParameter(IN IT_PROPS_K EY);
if ( propsFile != null )
{
String realPath = getServletContext().getRea lPath(prop sFile);
if ( realPath != null )
{
propsFile = realPath;
}
}
Properties p = new Properties();
p.load( new FileInputStream(propsFile) );
String path = p.getProperty("file.resour ce.loader. path");
if (path != null)
{
path = getServletContext().getRea lPath( path );
p.setProperty( "file.resource.loader.path ", path );
}
System.out.println(">>" + p.getProperty("file.resour ce.loader. path"));// THIS IS CORRECT !
path = p.getProperty("runtime.log ");
if (path != null)
{
path = getServletContext().getRea lPath( 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("Sample Servlet : parse error for template " + pee);
}
catch( ResourceNotFoundException rnfe )
{
System.out.println("Sample Servlet : template not found " + rnfe);
}
catch( Exception e )
{
System.out.println("Error " + e);
}
return outty;
}
}
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.FileNotFoundExcept
import java.util.Properties;
import java.util.Vector;
import javax.servlet.ServletConfi
import javax.servlet.http.HttpSer
import javax.servlet.http.HttpSer
import org.apache.velocity.Templa
import org.apache.velocity.contex
import org.apache.velocity.servle
import org.apache.velocity.app.Ve
import org.apache.velocity.except
import org.apache.velocity.except
import java.io.*;
import java.util.*;
public class Test extends VelocityServlet
{
protected Properties loadConfiguration(ServletC
throws IOException, FileNotFoundException
{
String propsFile = config.getInitParameter(IN
if ( propsFile != null )
{
String realPath = getServletContext().getRea
if ( realPath != null )
{
propsFile = realPath;
}
}
Properties p = new Properties();
p.load( new FileInputStream(propsFile)
String path = p.getProperty("file.resour
if (path != null)
{
path = getServletContext().getRea
p.setProperty( "file.resource.loader.path
}
System.out.println(">>" + p.getProperty("file.resour
path = p.getProperty("runtime.log
if (path != null)
{
path = getServletContext().getRea
p.setProperty("runtime.log
}
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("Sample
}
catch( ResourceNotFoundException rnfe )
{
System.out.println("Sample
}
catch( Exception e )
{
System.out.println("Error " + e);
}
return outty;
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
.. needless to say of course that the following line is in my velocity.properties :
file.resource.loader.path= D:\\webcon tainer\\me hdi_cms\\t emplates
regards,
Mehdi
file.resource.loader.path=
regards,
Mehdi
What directory is the properties file that is being loaded in?
ANd what is the base directory of your webapp?
ANd what is the base directory of your webapp?
ASKER
velocity.properties is in :
d:\classes\velocity.proper ties
contains :
file.resource.loader.path= D:\\webcon tainer\\me hdi_cms\\t emplates
and my webapp is in
D:\webcontainer\mehdi_cms
Cheers,
Mehdi
d:\classes\velocity.proper
contains :
file.resource.loader.path=
and my webapp is in
D:\webcontainer\mehdi_cms
Cheers,
Mehdi
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
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
ASKER
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(ServletC
throws IOException, FileNotFoundException
{
Properties p = new Properties();
p.load(ClassLoader.getSyst
System.out.print("WE ARE USING : " + p.getProperty(Velocity.FIL
return p;
}