English 中文(简体)
How to set up Velocity to find templates in the file system on Windows?
原标题:

I have a very simple Velocity application that works on Linux and MacOS and fails on Windows. The problem is with the resource locations. I just give it "/" to allow it to recognize file system paths, but on Windows that fails to work for "c:/....." pathnames. I suspect that there is a simpler solution to this, but what?

 velocityEngine = new VelocityEngine();
    // we want to use absolute paths.
    velocityEngine.setProperty("file.resource.loader.path", "/");
    try {
      velocityEngine.init();
    } catch (Exception e) {
      throw new MojoExecutionException("Unable to initialize velocity", e);
    }
最佳回答

I put velocity templates in the classpath and read them in with Class.getResourceAsStream.

It should go something like this:

// stuff.velocity is a file that lives directly under WEB-INF/classes 
// that contains the velocity template
InputStream inputStream = Class.getResourceAsStream("/stuff.velocity"); 
String template = readTemplateFromFile(inputStream);
VelocityContext context = new VelocityContext( );
// insert any parameters into context now
Writer writer = new StringWriter();
Velocity.evaluate( context, writer, "LOG", template );

and now writer should hold the result of applying the parameters to the template.

Will Glass comment below looks like a good thing to check out. When I was using velocity it was to generate notification emails, there were not a lot of them and I had the work farmed out to a separate thread so performance was not a big deal at the time.

问题回答

暂无回答




相关问题
how to use DynamicResource in the code behind?

I d like to be able to set a property to a dynamic resource programmatically. myControl.Property = this.Resource[key] is not a valid response, since if the resource with the key key is replaced, ...

PHP - Foreach loops and ressources

I m using a foreach loop to process a large set of items, unfortunately it s using alot of memory. (probably because It s doing a copy of the array). Apparently there is a way to save some memory with ...

C# Play MP3 from Resources with MCI or WMP control?

I have a MP3 file in my Resources of Visual C#. I m trying to find out if there is a way I can play this MP3 in a Windows Media Player control or with MCI, I m not particular. I m fairly new to C#. ...

Convert files of any types to a file with c strings

Please suggest a small command-line utility (for Windows) to convert files from particular directory to a valid c file. Maybe it can be done just with batch commands? The resulting file should look ...

How to sort ResourceSet in C#

I have a resource file named filetypes.resx. Some how I figured out to bind the resource values to dropdownlist, but I don t know how to sort the values of ResourceSet. Here is what I did so far, ...

热门标签