栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Velocity Unable to find resource

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Velocity Unable to find resource

在maven的velocity项目中将resources下的模板资源文件打包。

将velocity模块化引入到其它项目时报Unable to find resource '';Velocity.getTemplate获取资源文件出错!

VelocityContext velocityContext = new VelocityContext(context.toMap());
Template template = Velocity.getTemplate("mybatis\Model.java.vm", "UTF-8")
try (StringWriter writer = new StringWriter()) {
    template.merge(velocityContext, writer);
    return writer.toString();
} catch (IOException e) {
    e.printStackTrace();
}

解决方案1:

配置maven的build(可选择其它方式配置)

    
        
            
                src/main/resources
            
        
    

即可。

解决方案2:

使用变量编译模板。

final String templateContent = "package com.sicex.db.entity.${table.classPackage};n" +
            "n" +
            "import com.sicex.db.annotation.Column;n" +
            "import com.sicex.db.annotation.Key;n" +
            "import com.sicex.db.annotation.Table;n" +
            "import com.sicex.db.type.KeyType;n" +
            "import lombok.Data;n" +
            "import org.apache.ibatis.type.JdbcType;n" +
            "n" +
            "import java.util.Date;n" +
            "n" +
            "n" +
            "@Datan" +
            "@Table(name = "${table.tableCode}")n" +
            "public class ${table.classCode} {n" +
            "#foreach (${column} in ${table.columns})n" +
            "    n" +
            "    #if (${column.primaryKey})n" +
            "@Key(${column.primaryRule})n" +
            "    #endn" +
            "@Column(name = "${column.columnCode}",jdbcType = ${column.jdbcType})n" +
            "    private ${column.fieldType} ${column.fieldCode};n" +
            "#endn" +
            "}n";

编译方式:

VelocityContext velocityContext = new VelocityContext(context.toMap());
VelocityEngine velocityEngine = new VelocityEngine();
try (StringWriter writer = new StringWriter()) {
    velocityEngine.evaluate(velocityContext,writer,"",templateContent);
    return writer.toString();
} catch (IOException e) {
    e.printStackTrace();
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/302046.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号