使用velocity的路径问题

总是报org.apache.velocity.exception.ResourceNotFoundException: Unable to find resource 'simple.vm'
我真见鬼了。我总觉得velocity找模板的路径很蹊跷
请知道的大哥指点一下啊
[code="java"]
public static void exportFile(){
VelocityEngine ve=new VelocityEngine();
Properties p = new Properties();
p.setProperty("resource.loader", "classes");//这里对不对?
p.setProperty("class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");//这里对不对?
p.setProperty(Velocity.OUTPUT_ENCODING, "UTF-8");

p.setProperty(Velocity.INPUT_ENCODING, "UTF-8");
try {
ve.init(p);
Template t=ve.getTemplate("simple.vm","utf-8");

        VelocityContext context = new VelocityContext(); 
        context.put("name", "张三李四王五");
        context.put("project", "Jakarta");

        PrintWriter writer = new PrintWriter("D:\\test.html","UTF-8");
        t.merge(context, writer);
        System.out.println(writer.toString());
        writer.flush();
        writer.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

[/code]
以上代码需要找到模板输出一个html文件,可是一直找不到模板,怎么回事?下图是我的路径,可以看到在WebRoot下有个vm文件夹,
在WEB-INF下也有个vm文件夹,还有在tomcat的Tomcat 5.5\webapps\myProject\WEB-INF\classes\vm\simple.vm也有一个vm文件夹,可就是报错,不管你写在哪里!!!怎么回事啊?谢谢大哥们了

[img]http://dl.iteye.com/upload/attachment/270641/5e116fd3-9824-3b41-85cc-e148ae21d85e.png[/img]

[code="java"]
public synchronized InputStream getResourceStream(String name)
throws ResourceNotFoundException
{
InputStream result = null;

if ((name == null) || (name.length() == 0))
{
  throw new ResourceNotFoundException("No template name provided");
}

try
{
  ClassLoader classLoader = super.getClass().getClassLoader();
  result = classLoader.getResourceAsStream(name);
}
catch (Exception fnfe)
{
  throw new ResourceNotFoundException(fnfe.getMessage());
}

return result;

}

[/code]

打开了源代码粗粗看了下ClasspathResourceLoader中的代码。 自己看代码关键是
ClassLoader classLoader = super.getClass().getClassLoader();
result = classLoader.getResourceAsStream(name);

这两行代码。是在你的Classpath下去load, 所以必须保证vm文件存在于WEB-INF/classes 下

我们项目中主要就是使用它做为mail的发送模板在使用。

发现在tomcat和weblogic下这个路径解析方法还是不一样的。
项目中使用了如下的方法来完成这部分的处理的。 希望对你有帮助。

public String getMailContent(String templateName, Map model) {
String mailContent = null;
try {
//try get the mail template from the jar
if (com.opensymphony.xwork2.util.ClassLoaderUtil.getResource(
templateName, this.getClass()) != null) {
mailContent = VelocityEngineUtils.mergeTemplateIntoString(
velocityEngine, templateName, "UTF-8",
model);
}else{
//if mail template not exist in mailtemplate.jar,
//then get mail template from mailtemplate path

mailContent = VelocityEngineUtils.mergeTemplateIntoString(
velocityEngine, "mailtemplate\" + templateName, "UTF-8",
model);
}
} catch (VelocityException e) {
//if can not get the mailtemplate, record log information.
log.error("Exception happened at Class "
+ getClass().getSimpleName() + " When load mail template."
+ " Exception information: \t\n " + e.getMessage());
}
return mailContent;
}