Velocity例子

来源:互联网 发布:杭州百替生物数据造假 编辑:程序博客网 时间:2024/05/16 16:15
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Map;
import java.util.Properties;
 
import org.apache.commons.lang.ObjectUtils;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.app.VelocityEngine;
 
import com.gzdec.common.config.AppConfig;
import com.gzdec.common.config.AppConfigException;
import com.gzdec.common.jdbc.core.DataAccessException;
 
 
/**
* velocity操作类
 * @author YangZhiFeng   
 * @version 1.0   
 * @created 2012-8-7 下午03:10:31  
 */   
public class VelocityUtil {
 
    /***
     * 生成模板
     * @param filePath:生成的文件路径    vmPath:模板所在全路径    context:参数
     * @throws Exception
     * @throws DataAccessException
     */
    public void createVmByPath(String filePath,String vmPath,VelocityContext context) throws Exception{
 
        // 创建引擎 
        VelocityEngine ve=new VelocityEngine();  
        //设置模板加载路径,这里设置的是class下  
        ve.setProperty(Velocity.RESOURCE_LOADER, "file");
        ve.setProperty("file.resource.loader.class", "org.apache.velocity.runtime.resource.loader.FileResourceLoader");
        ve.setProperty("file.resource.loader.path", ObjectUtils.toString(AppConfig.getProperty("rootPath")));
        //进行初始化操作  
        ve.init();
        try {
            //进行初始化操作
            ve.init();
            //加载模板,设定模板编码
            Template t=ve.getTemplate(vmPath,"gbk");
            //设置输出
            PrintWriter writer = new PrintWriter(filePath);
            //将环境数据转化输出
            t.merge(context, writer); 
            writer.close();  
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
    /***
     * 生成模板
     * @param filePath:生成的文件路径    fileName:模板所在文件夹名    vmName:模板名称        context:参数
     * @throws Exception
     * @throws DataAccessException
     */
    public void createVmByName(String filePath,String fileName,String vmName,VelocityContext context) throws Exception{
 
        // 创建引擎 
        VelocityEngine ve=new VelocityEngine();  
        //设置模板加载路径,这里设置的是class下  
        ve.setProperty(Velocity.RESOURCE_LOADER, "file");
        ve.setProperty("file.resource.loader.class", "org.apache.velocity.runtime.resource.loader.FileResourceLoader");
        ve.setProperty("file.resource.loader.path", ObjectUtils.toString(AppConfig.getProperty("rootPath")));
        //进行初始化操作  
        ve.init();
        try {
            //进行初始化操作
            ve.init();
            //加载模板,设定模板编码
            Template t=ve.getTemplate(Constants.VM_TEMPLATE_PATH+"/"+fileName+"/"+vmName,"gbk");
            //设置输出
            PrintWriter writer = new PrintWriter(filePath);
            //将环境数据转化输出
            t.merge(context, writer); 
            writer.close();  
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
    /***
     * 测试方法
     * @return
     * @throws Exception
     * @throws DataAccessException
     */
    public static void main(String[] args) throws Exception {
        VelocityUtil f = new VelocityUtil();
        VelocityContext context = new VelocityContext();
        context.put("name", "张三");
        context.put("project", "Jakarta");
             }
 
}
原创粉丝点击