velocity模板路径在不同项目中的获取问题

     以前就遇到过在linux上和在windows上获取文件路径的方式不一样;因为我平常在windows上开发项目,最后部署到docker容器中就会报无法找到模板路径;我在windows本地是以springboot项目启动,部署到docker中是以jar包方式启动,可见这两种方式会影响模板路径的获取。

      这次的问题跟以前稍微不一样,都是部署在docker中,以前是war包运行,现在改成jar包运行,也是报org.apache.velocity.exception.ResourceNotFoundException: Unable to find resource

1.     tomcat项目war包部署运行获取方式如下:

        VelocityEngine velocityEngine = new VelocityEngine();
        Properties properties = new Properties();
        String dir = MailSendService.class.getResource("/").getPath();

        String  path = "static/template";

        Sring  name = "test.vm";
        properties.setProperty(Velocity.FILE_RESOURCE_LOADER_PATH, dir + path);
        velocityEngine.init(properties);
        Template template = velocityEngine.getTemplate(name, "utf-8");

    模板文件一般存放在resource目录下的static/template文件夹下,war包运行中这么获取是没问题的,把项目变成jar包部署运行就报错了;所以修改代码如下:

2.jar包中获取方式
        VelocityEngine velocityEngine = new VelocityEngine();
        Properties properties = new Properties();
        properties.setProperty("resource.loader", "class");
        properties.setProperty("class.resource.loader.class",
                 "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
        velocityEngine.init(properties);

        String  path = "static/template";

        Sring  name = "test.vm";

        Template template = velocityEngine.getTemplate(path + name, "utf-8");

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值