Now this is really pain in the *
After solving this i can help you setting up your properties for web project
For a start take a look here:
Nabble - Velocity - User - WebappResourceLoader: ResourceNotFoundException
- -
Same Velocity code won't work in diff projects
and configuration is totally same ?!?
Everyone is pointing to properties of Velocity Engine
that has to be set manually like, before calling init():
...
ve.setProperty("file.resource.loader.class",Classp athResourceLoader.class.getName());
...
So my helloworld.vm resides in package called volocity
next to HelloWorld class that uses it.
Contenet of helloworld.vm is
|
Code:
|
Hello $name! Welcome to Velocity! |
|
Code:
|
package velocity;
import java.io.File;
import java.io.FileReader;
import java.io.StringWriter;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;
public class HelloWorld
{
public static void main( String[] args ) throws Exception
{
String path = "C:\\workspace\\Sve\\src\\velocity\\helloworld.vm";
File f = new File(path);
FileReader fr = new FileReader(f);
int a = 0;
while( (a = fr.read()) != -1 ){
System.out.println(a);
}
/* first, get and initialize an engine */
VelocityEngine ve = new VelocityEngine();
ve.init();
Template t = ve.getTemplate( "helloworld.vm" );
System.out.println(ve.resourceExists("C:\\workspace\\Sve\\src\\velocity\\helloworld.vm"));
/* create a context and add data */
VelocityContext context = new VelocityContext();
context.put("name", "World");
/* now render the template into a StringWriter */
StringWriter writer = new StringWriter();
t.merge( context, writer );
/* show the World */
System.out.println( writer.toString() );
}
} |
That code works in one project and won't work in other?!
Both are standalone small projects, not much *.jars on classpath.
I am using same velocity and commons jars in both and same
system runtime. Using velocity-1.6.2.
In debugger just after calling ve.init()
35 properties are set (ve => _ri => configuration => Extended properties)
Among them there are
file.resource.loader.path and similar
that probably has to be set to some other values in order to make this work
good luck!