velocity模板加载的三种形式

文针对velocity模板加载的三种方式进行说明

一:velocity默认加载方式(文件加载)  (下面是针对文件加载的目录结构)

 

 

1.直接读取资源文件方式

 

public class VelocityTemplate {
    public static String getVelocityTemplate(String basePath) throws Exception {
        String sysRoot = VelocityTemplate.class.getResource("").getPath();
        Properties properties = new Properties();
        //设置velocity资源加载方式为file
        properties.setProperty("resource.loader", "file");
        //设置velocity资源加载方式为file时的处理类
        properties.setProperty("file.resource.loader.class","org.apache.velocity.runtime.resource.loader.FileResourceLoader");
        properties.put("input.encoding", "UTF-8");
        properties.put("output.encoding", "UTF-8");
        //实例化一个VelocityEngine对象
        VelocityEngine velocityEngine = new VelocityEngine(properties);
        //实例化一个VelocityContext
        VelocityContext velocityContext = new VelocityContext();
        //向VelocityContext中放入键值
        context.put("username", "张三");
        context.put("password", "123456789");
        context.put("age", "20");
        //实例化一个StringWriter
        StringWriter writer=new StringWriter();
        Template template = velocityEngine.getTemplate(sysRoot+basePath, "UTF-8");
        template.merge(velocityContext, stringWriter);
        return stringWriter.toString();
    }
}

 2  从类路径加载模板文件

 

package com.velocity.test;
import java.io.StringWriter;
import java.util.Properties;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;
/**
 * 从class(类路径)中加载模板文件
 */
public class LoaderFromClass {
    public static void main(String[] args) throws Exception{
        //初始化参数
        Properties properties=new Properties();
        //设置velocity资源加载方式为class
        properties.setProperty("resource.loader", "class");
        //设置velocity资源加载方式为file时的处理类
        properties.setProperty("class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
        //实例化一个VelocityEngine对象
        VelocityEngine velocityEngine=new VelocityEngine(properties);
        
        //实例化一个VelocityContext
        VelocityContext context=new VelocityContext();
        //向VelocityContext中放入键值
        context.put("username", "张三");
        context.put("password", "123456789");
        context.put("age", "20");
        context.put("address", "陕西西安"); 
        context.put("blog", "http://blogjava.net/sxyx2008");
        //实例化一个StringWriter
        StringWriter writer=new StringWriter();
        //从src目录下加载hello.vm模板
        //假若在com.velocity.test包下有一个hello.vm文件,那么加载路径为com/velocity/test/hello.vm
        velocityEngine.mergeTemplate("com/velocity/test/hello.vm", "gbk", context, writer);
        //velocityEngine.mergeTemplate("hello.vm", "gbk", context, writer);
        System.out.println(writer.toString());
    }
}

 

从jar文件中加载模板文件

public class VelocityTemplate {
    public static String getVelocityTemplate(String basePath) throws Exception {
        String sysRoot = VelocityTemplate.class.getResource("").getPath();
        //初始化参数
        Properties properties=new Properties();
        //设置velocity资源加载方式为jar
        properties.setProperty("resource.loader", "jar");
        //设置velocity资源加载方式为file时的处理类
        properties.setProperty("jar.resource.loader.class", "org.apache.velocity.runtime.resource.loader.JarResourceLoader");
        //设置jar包所在的位置
        properties.setProperty("jar.resource.loader.path", "jar:file:/F:/quicksure_Server_Provider.jar");
        properties.put("input.encoding", "UTF-8");
        properties.put("output.encoding", "UTF-8");
        //实例化一个VelocityEngine对象
        VelocityEngine velocityEngine = new VelocityEngine(properties);
        //实例化一个VelocityContext
        VelocityContext velocityContext = new VelocityContext();
        //向VelocityContext中放入键值
        context.put("username", "张三");
        context.put("password", "123456789");
        context.put("age", "20");
        //实例化一个StringWriter
        StringWriter writer=new StringWriter();
        Template template = velocityEngine.getTemplate("com/quicksure/insurance/velocity/template/"+basePath, "UTF-8");
        template.merge(velocityContext, stringWriter);
        return stringWriter.toString();
    }
}

 

 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
简介: 工程自动获取和封装用户自定义参数以及数据库表信息,并通过模板配置文件将生成代码文件直接输出到应用工程目录下,达到从数据库表到页面增删改查过程代码一键生成. 工程提供所有源码,包括第三方jar包也附带源码包 注意事项: 1.需要一定的java开发基础. 2.要有java开发的集成环境,本工程没有界面,很多功能都是通过直接注释,或增加一些代码完成. 3.JDK版本必须是1.6以上,否则会报版本错误.当然也可以更换工程依赖的jar,使用一些低版本的jar包 使用说明: 1. 修改数据库连接参数:详见jdbc.properties 2. 修改或增加数据库字段类型与编译语言数据类型映射:详见dm2java.properties 3. 修改模板配置信息:详见vm.xml 4. 编写用户模板,参考/templates目录下的文件 5. 开启关闭/备份: 调用FileUtilsExt.backup(String[] fileList, String backupDir)方法 6. 还原备份: 调用FileUtilsExt.recover(String backupDir)方法. 7. 从数据库表合成文件: 调用PlayTemplates的main方法 8. 从EXCEL和成文件: 调用ExcelUtils的main方法 详细说明: 1.自动封装用户参数 参数配置文件params.properties有三类参数: 1.1 以array.为前缀参数(可设定设定多个)且用逗号分隔,将封装成数组对象,可循环输出 例如: array.names=xixi, haha 模版调用: #foreach($item in $names) $item #end 输出:xixi haha 1.2 以single.为前缀的参数(可设定设定多个),可在模板中直接调用输出 例如: single.name=xixi 模版调用: $name 输出:xixi 1.3 无任何前缀的参数,为工程要强制使用的参数,必须填写 2. 封装数据库表参数,目前只支持达梦数据库(我们公司自个的数据库),其他数据库以后再完善 如果各位想用其他数据库,可以自己加载其他数据库的驱动,并修改一下DbOption类下的getTableColumns(String)方法中的查询表信息方法 以及添加类似dm2java.properties数据库类型到java数据类型的映射文件,不同数据库的方言和数据映射太烦了. 当然,也可以联系我^_^ 自动从数据库中获取表名/表注释/列名/列注释/列类型等等相关信息 示例:由角色表生成domain对象 模版: package $!{package}.domain; import java.util.Date; /** * $!system * @author $!author * @version $!version * @date $!dateTool.format('yyyy-MM-dd', ${date}) */ public class $!{tableAlias}{ #foreach($item in $columnList) private $!item.data_type $!item.column_name.toLowerCase(); // $!item.column_label #end #foreach($item in $columnList) #set($name = $!item.column_name.toLowerCase()) #set($upperName = $!{stringTool.firstUpperCase($name)}) public void set$!{upperName}($!item.data_type $name){ this.$name=$name; } public $

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值