velocity-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Jason Pettiss <>
Subject Re: VelocityServlet and Properties file
Date Mon, 10 Oct 2005 19:07:37 GMT
I have the same situation... this is what I do in my loadConfiguration 

        //Configuration file to be read is specified as
        //the init param ''
        Properties p = super.loadConfiguration(config);

        //For ease of use, our templates are found in our webapp,
        //just as if they were JSPs
        String path = makeServletContextPath(config.getServletContext());

        //Tell Velocity where we expect our templates to be
        p.setProperty(Velocity.FILE_RESOURCE_LOADER_PATH, path);
        return p;

Then I just put my templates in the same place as my JSP pages and if a 
request comes in for /dir/foo.vtl I just serve that right up.

The only other thing you'll need is that you tell the servlet in your 
web.xml where to find the default properties bundle:


And the only thing I need in my is:

Since I have a custom logger.  The rest of the file loader stuff except 
for the modfication interval you should just remove since I think those 
are the defaults anyway, except for the path, which we're setting with 
our loadConfiguration method.

You might look at the implementation for the super loadConfiguration 
method-- but it may require that actually end with a 
".properties" extension.  I doubt it, but if you do all this and it 
still seems your file is being ignored, try that.


Andrew Mason wrote:

>Ok, I know VelocityServlet is depricated in favour of the VelocityViewServlet 
>but i started my web-app before VelocityServlet became deprecated so to keep 
>consistency (for now) i'm using it.
>Anyway my problem is that my servlet can't seem to find my template. I've 
>tried putting it in / , /obb, /obb/WEB-INF but it doesn't seem like it wants 
>to pick it up at all. Also i'm not sure that its picking up the 
> file. 
>I got around this problem in the other servlets by overwriting the 
>loadConfig() and making a new instance of Property. This was fine until i 
>actually wanted to specify some of my own and it would be handy to have them 
>all in one place.
>thanks in advance
>public class EditPage extends VelocityServlet{
>	public Template handleRequest(HttpServletRequest request,
>		HttpServletResponse response,Context context) {
>		// stuff in here
>		//Merge the Template now
>			try{
>                		String templateName="edit.vm";
>				template = getTemplate(templateName);
>			}
>			catch(ResourceNotFoundException rnfe){
>		                System.out.println(templateName);
>				rnfe.printStackTrace();
>			}
>			catch(Exception e){
>				e.printStackTrace();
>			}
>		return template;
>	}
>and my directory structure works like this (slightly different to normal as it 
>allows me to use mod_jk to map to / )
>	-/myapp/
>			/obb/
>			  	WEB-INF/
>						classes/  
>						lib/  
>						- 
>						- web.xml
>	- obb.war
>	- edit.vm
>	- index.vm
>resource.loader = file
>file.resource.loader.description = Velocity File Resource Loader
>file.resource.loader.class = 
>file.resource.loader.path = .
>file.resource.loader.cache = false
>file.resource.loader.modificationCheckInterval = 0
>To unsubscribe, e-mail:
>For additional commands, e-mail:

To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message