velocity学习-第一个例子就错,Unable to find resource

http://linjia880714.iteye.com/blog/1312895

velocity学习-第一个例子就错,Unable to find resource

博客分类:  apache
 

本人还是菜鸟,希望各位大虾指教!!

 

刚开始学习velocity就报错org.apache.velocity.exception.ResourceNotFoundException: Unable to find resource ,死活解决不了,最后看了下源码,终于找到错误所在了

 

从官网copy自己稍微改下的例子


velocity学习-第一个例子就错,Unable to find resource_第1张图片

开始是天真的放在同一目录下的相对路径,结果一次又一次的Unable to find resource

 

最后实在受不了,追踪了下velocity的源码

在org.apache.velocity.runtime.resource.loader.FileResourceLoader

Java代码   收藏代码
  1. public boolean resourceExists(String name)  
  2.   {  
  3.       if (name == null)  
  4.       {  
  5.           return false;  
  6.       }  
  7.       name = StringUtils.normalizePath(name);  
  8.       if (name == null || name.length() == 0)  
  9.       {  
  10.           return false;  
  11.       }  
  12.   
  13.       int size = paths.size();  
  14.       for (int i = 0; i < size; i++)  
  15.       {  
  16.           String path = (String)paths.get(i);  
  17.           try  
  18.           {  
  19.               File file = getFile(path, name);  
  20.               if (file.canRead())  
  21.               {  
  22.                   return true;  
  23.               }  
  24.           }  
  25.           catch (Exception ioe)  
  26.           {  
  27.               String msg = "Exception while checking for template " + name;  
  28.               log.debug(msg, ioe);  
  29.           }  
  30.       }  
  31.       return false;  
  32.   }  
 

最后是变成了"./Test1.vm"

 


velocity学习-第一个例子就错,Unable to find resource_第2张图片

放在这个目录就没事了。

 

“.”代表的应该是项目的根路径

 

最后试了下使用“/Test.vm”,“./Test.vm”和“Test.vm”一样

你可能感兴趣的:(前端模板,freemarker,Velocity)