美文网首页
org.apache.velocity.exception.Re

org.apache.velocity.exception.Re

作者: ChanHsu | 来源:发表于2017-04-25 16:20 被阅读301次

    VelocityEngine这个类,调用其getTemplate方法时,里面的路径设置一直有问题,非常坑啊

    VelocityEngine ve = new VelocityEngine();
    ve.init();
    Template t = ve.getTemplate(fileName);
    

    实验多次后发现上面的fileName必须是直接位于项目目录下的路径,直接写绝对路径都不行例如:

    template   =   ve.getTemplate("welcome.vm"); 
    

    welcome.vm放到你的项目根目录,也就是x:/tomcat/webapps/yourProject/welcome.vm 如果是:

    template   =   ve.getTemplate("abc/welcome.vm"); 
    

    welcome.vm放到你的项目的位置是x:/tomcat/webapps/yourProject/abc/welcome.vm 但是这样一来,有些人的本地开发和服务器时,模板文件的位置就不太一样,就会出现路径不对而throw出org.apache.velocity.exception.ResourceNotFoundException异常后来又实验了半天,终于发现以下方法可以解决问题,可以用绝对路径来配置,我这里是动态获取路径我的模板文件是放在资源目录的 "template" 目录下,但把项目发布到solaris系统的websphere上路径就很乱了,所以动态获取路径可以解决

    String fileDir = 我的类名.class.getResource("/template").getPath();
    VelocityEngine ve = new VelocityEngine();
    Properties properties = new Properties();
    properties.setProperty(ve.FILE_RESOURCE_LOADER_PATH, fileDir); //此处的fileDir可以直接用绝对路径来//指定,如"D:/template",但记住只要指定到文件夹就行了
    ve.init(properties); //初始化
    Template t = ve.getTemplate("welcome.vm");//此处只要指明文件名就可以了.**
    

    相关文章

      网友评论

          本文标题:org.apache.velocity.exception.Re

          本文链接:https://www.haomeiwen.com/subject/yzbvzttx.html