On 7/12/07, Joe Kramer <[EMAIL PROTECTED]> wrote:
Hello,
I've spent good 10 hours trying to load template in webapp.
First I tried
 resource.loader= file
and putting template into any posible directory, WEB-INF, classes,
lib, web app root.
Also tried to use
 resource.loader=class
Putting template in all possible places. Including create template.jar
and putting it in /lib.
It's always "Unable to find resource 'template.vm'"

So I nothing helps using out-of-the-box methods.
I wrote class ResourceLoader extends
org.apache.velocity.runtime.resource.loader.ResourceLoader

and put resource.loader=custom

I added Log message into getResourceStream(), the method to be called
when template is requested.
Java code:
VelocityEngine ve = new VelocityEngine();
ve.init();
Template template = Velocity.getTemplate("template.vm");

This is all wrong.   You create a VelocityEngine, then initialize it
without your custom properties, then try to get a template from the
Velocity singleton!!  Before we can worry about the resource loaders,
we need to make sure you are using Velocity properly period.  Change
the code above to:

VelocityEngine ve = new VelocityEngine();
ve.init("path/to/your/properties/file");
Template template = ve.getTemplate("template.vm");

oh, and in a web application, you should really only be using the
ClasspathResourceLoader or the WebappLoader from VelocityTools.   And
to shorten your test cycle, did you know you can use multiple resource
loaders?  Try:

resource.loader = webapp, classpath
webapp.resource.loader.class =
org.apache.velocity.tools.view.servlet.WebappLoader
classpath.resource.loader.class =
org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader

then, you can put your template.vm file in the root folder of the
webapp if you want the WebappLoader to find it and/or in the
/WEB-INF/classes/ directory (as Will said) if you want the
ClasspathResourceLoader to find it.  Note that with the properties
above, the resource manager will always first try to find a template
via the webapp loader, and only fall back to checking the classpath
loader if the webapp one can't find it.

What do I get in log:
 INFO   - getResourceStream() load VM_global_library.vm
ERROR  - Unable to find resource 'template.vm'

It looks for VM_global_library.vm! Why???

Will has said this, but i'll repeat anyway: ignore the
VM_global_library.vm thing.  It's a default setting that won't hurt
anything.

In velocity.properties it's even commented out:
# velocimacro.library = VM_global_library.vm
And VM_global_library.vm is present in path just in case.
I don't get any messages that VM_global_library.vm not found, I gt
message that requested template not found but request to
ResourceLoader wasn't made for that template.

in that case, i'm not entirely convinced that your custom properties
have been loaded.  especially since your code sample shows you
init()'ing the engine without your properties and then asking the
singleton (which is an entirely separate engine underneath) for the
template.  What do your log files say for Velocity startup?  We need
to make sure that your properties are being loaded.



How do find template resources in webapp?
Why even that I implemented my own resourceLoader, it looks for
VM_global_library.vm and returns that requested template not found?

Thanks.

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



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

Reply via email to