`
phantom
  • 浏览: 160655 次
社区版块
存档分类
最新评论

Velocity 中执行业务

阅读更多

package com.jh.core.util;

import java.io.StringWriter;
import java.util.Map;
import java.util.Properties;

import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.exception.MethodInvocationException;
import org.apache.velocity.exception.ParseErrorException;

import com.jh.app.util.Globle;

public class VelocityUtil
{
   static VelocityEngine ve=null;
   public static VelocityEngine velocity()
   {
   if(ve!=null){
    return ve;
   }
      ve = new VelocityEngine();
      Properties p = new Properties();
      p.setProperty(Velocity.RUNTIME_LOG, "velocity_jh.log");
      p.setProperty(Velocity.FILE_RESOURCE_LOADER_PATH, Globle.VM_PATH);
      try
      {
         ve.init(p);
      }
      catch(Exception e)
      {
         e.printStackTrace();
      }
      return ve;
   }

   public static void main(String args[])
   {
      try
      {
         Velocity.init();
      }
      catch(Exception e)
      {

         System.out.println("Problem initializing Velocity : " + e);
         return;
      }
      VelocityContext context = new VelocityContext();
       TTT tt= new TTT();
       tt.setSs("vvvvvvvv");
       tt.setQq("qqq");
      context.put("tt", tt);
      context.put("name", "Velocity");
      context.put("project", "Jakarta");
     
      StringWriter w = new StringWriter();
//      try
//      {
//         velocity().mergeTemplate("example2.vm", context, w);
//      }
//      catch(Exception e)
//      {
//         System.out.println("Problem merging template : " + e);
//      }
      System.out.println(" template : " + w);
     // String s = "We are using $project $name to render this.\n #parse(\"example2.vm\")";
      String s = " ${tt.qq}_dd  ";
     
      w = new StringWriter();
      try
      {
         velocity().evaluate(context, w, "mystring", s);
      }
      catch(ParseErrorException pee)
      {
         System.out.println("ParseErrorException : " + pee);
      }
      catch(MethodInvocationException mee)
      {
         System.out.println("MethodInvocationException : " + mee);
      }
      catch(Exception e)
      {
         System.out.println("Exception : " + e);
      }
      System.out.println(" string : " + w);
   }

   public static String evaluate(String vmText, Map map, String logTag)
   {
      // System.out.println(vmText);
      StringWriter w = new StringWriter();
      VelocityContext context = new VelocityContext();
      if(map != null)
      {
         java.util.Iterator it = map.keySet().iterator();
         while(it.hasNext())
         {
            Object obj = it.next();
            if(obj instanceof String)
               context.put((String) obj, map.get(obj));
         }
      }
      try
      {
         velocity().evaluate(context, w, logTag, vmText);
      }
      catch(ParseErrorException pee)
      {
         System.out.println("ParseErrorException : " + pee);
      }
      catch(MethodInvocationException mee)
      {
         System.out.println("MethodInvocationException : " + mee);
      }
      catch(Exception e)
      {
         System.out.println("Exception : " + e);
      }
      return w.toString();
   }

   public static String mergeVM(String vmFile, Map map)
   {
      StringWriter w = new StringWriter();
      VelocityContext context = new VelocityContext();
      if(map != null)
      {
         java.util.Iterator it = map.keySet().iterator();
         while(it.hasNext())
         {
            Object obj = it.next();
            if(obj instanceof String)
               context.put((String) obj, map.get(obj));
         }
      }
      try
      {
         velocity().mergeTemplate(vmFile, context, w);
      }
      catch(ParseErrorException pee)
      {
         System.out.println("ParseErrorException : " + pee);
      }
      catch(MethodInvocationException mee)
      {
         System.out.println("MethodInvocationException : " + mee);
      }
      catch(Exception e)
      {
         System.out.println("Exception : " + e);
      }
      return w.toString();
   }

}

 

 

package com.jh.app.util;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.exception.ResourceNotFoundException;

 

public class VelocityUtil
{
 public  static String GLOBLE_VM_PATH = "/usr/bj_app/ROOT";
    private static VelocityEngine ve =null;
    synchronized public  static VelocityEngine velocity()
 {
   if(ve!=null) return ve;
   ve = new VelocityEngine();
  Properties p = new Properties();
  p.setProperty(Velocity.RUNTIME_LOG, "velocity_jh.log");
  p.setProperty(Velocity.FILE_RESOURCE_LOADER_PATH, GLOBLE_VM_PATH);
  try
  {
   ve.init(p);
  }
  catch (Exception e)
  {
   e.printStackTrace();
  }
  return ve;
 }
    public static void main(String args[]){
     Map map=new HashMap();
     map.put("Name", "王立昌");
     map.put("util", new UtilObj());
     System.out.println(evaluate("Hello $Name !" +
       "" +
       "  #set ( $dao = $util.getDao(\"cms\") )  \n"+
       "  #set ( $sql = \"select * from News_tab  order by NewsId desc limit 10  \")  \n"+
       "  #set ( $className = \"com.jh.app.tag.app.News\")  \n"+
       "     #set ( $clazz = $util.classForName($className) )  \n" +
       "         $sql \n"+
       "     #set ( $list = $dao.getObjList( $sql,$clazz))      \n"+
       "     #foreach( $obj in $list )  \n"+
       "      $obj.Title \n"+
       "  #end \n"+
       "       \n"
      
       ,map,"test"));
    }


 public static String evaluate(String vmText, Map map, String logTag)
 {
  
  System.err.println("########################");
  System.err.println(vmText);
  System.err.println("########################");
  StringWriter w = new StringWriter();
  VelocityContext context = new VelocityContext();
  if (map != null)
  {
   java.util.Iterator it = map.keySet().iterator();
   while (it.hasNext())
   {
    Object obj = it.next();
    if (obj instanceof String)
     context.put((String) obj, map.get(obj));
   }
  }

  try
  {
   velocity().evaluate(context, w, logTag, vmText);
  }
  catch (ResourceNotFoundException e)
  {
   //该处理是为了避免包含的文档不存在而做的处理.
   String msg = "org.apache.velocity.exception.ResourceNotFoundException: Unable to find resource ";
   String str = e.toString();
   if (str.indexOf(msg) < 0)
    return "";
   str = str.replaceAll(msg, "");
   str = str.replaceAll("'", "");
   String fileName = GLOBLE_VM_PATH + "/" + str;
   writeStrFile(fileName, "");
   return evaluate(vmText, map, logTag);
  }  
  catch (Exception e)
  {
   e.printStackTrace();
  }
  return w.toString();
 }

 public synchronized static boolean writeStrFile(String strFileName, String strContent)
 {
  if (strFileName.trim().equals(""))
   return true;

  int indx = strFileName.lastIndexOf("/");
  if (indx >= 0)
  {
   String strDir = strFileName.substring(0, indx);
   File f = new File(strDir);
   if (!f.exists())
    f.mkdirs();
  }
  
  try
  {
   FileOutputStream fos = new FileOutputStream(strFileName);
   try
   {
    OutputStreamWriter o = new OutputStreamWriter(fos, "GBK");
    o.write(strContent);
    fos.flush();
    o.close();
   }
   finally
   {
     fos.close();
   }
  }
  catch (Exception e)
  {
   e.printStackTrace();
  }
  
  return true;
 }

 public static String mergeVM(String vmFile, Map map)
 {
  StringWriter w = new StringWriter();
  VelocityContext context = new VelocityContext();
  if (map != null)
  {
   java.util.Iterator it = map.keySet().iterator();
   while (it.hasNext())
   {
    Object obj = it.next();
    if (obj instanceof String)
     context.put((String) obj, map.get(obj));
   }
  }
  try
  {
   velocity().mergeTemplate(vmFile, context, w);
  }
  catch (Exception e)
  {
   e.printStackTrace();
  }
  return w.toString();
 }

}

分享到:
评论

相关推荐

    Velocity1.4 学习指南中文版

    Velocity 的主要目标是通过模板生成格式文档. 因此, Velocity自身不提供任何Web相关的功能. 当你需要开发Web应用时,...需要一个框架来接收 HTTP请求和, 处理用户认证,执行业务逻辑调用,最后生成应答内容返回给客户端。

    基于jbpm与activiti的工作流平台技术架构介绍

    这大大方便了业务流程在所有的系统中进行流程开发的工作,简化了以往开发流程业务的代码固死的做法,把程序员或业务员真正从流程业务中解放出来。 【图七】在线流程管理 【图八】在线流程设计 6.报表管理 系统...

    大数据与云计算.docx

    "建立在对互联网、物联网等渠道广泛大量数 据资源收集基础上的数据存储、价值提炼、智能处理和分发的信息服务业",或者如IT巨头概括大数据战略为:"致力于让所有用户能够从几乎任何数据中获得可转换为业务执行的洞察...

    mybaits-plus,generator自动化生产代码

    根据数据库表设计,自动化生产代码,完成增删改查分页功能,内含sql,Velocity 引擎模板更改(controller.java.vm,entity.java.vm,service.java.vm,serviceImpl.java.vm)执行Test方法,一键生成

    AutoEE_V1-自动智能开发平台免费下载

    AutoE_V2版本新增了代码模板扩展功能,开发人员可以根据业务需求自定义所需的代码模板,进行模块配置时选择自定义的代码模板,实现自动生成的代码完全符合自己的业务需求,使得开发平台能够适应于各种各样的业务...

    外文翻译 stus MVC

    Tags are one of the things that make Struts unique compared to other frameworks like Velocity. Note: "Think thin" when extending the Action class. The Action class should control the flow and not ...

    java8源码-asuka::bird:模板提供mybatiscodegenerator功能,程序是图形界面半自动化,修改模板可生成任意语言,任意代码风

    本工具只提供执行模板以及适配数据源的代码,使用者根据项目本身编程语言与编码规范,修改项目示例模板,生成完全符合项目组风格的单表业务代码,目前测试版本仅支持Mysql数据库,后续会尝试适配PostgreSQL,Elastic...

    Spring面试题

    Execute()从ActionForm对象中获取数据,完成业务逻辑,返回一个ActionForward对象,ActionServlet再把客户请求转发给ActionForward对象指定的jsp组件,ActionForward对象指定的jsp生成动态的网页,返回给客户。...

    大数据与Hadoop.doc

    区别于过去的海量数据,大数据的特点可以概况为4个V:Volume、Variety、V alue和Velocity,即大量、多样、价值密度低、快速。 第一,数据体量大。大数据一般指在10TB(1TB=1024GB)规模以上的数据量, 目前正在跃升...

    宏「Hiro」-crx插件

    即时协作,就像消息传递一样简单。...但是某些功能需要每月计划(这是我们的业务模式),而不是将数据出售给广告客户。 但是,您始终可以根据需要与任意数量的朋友共享任意数量的笔记。 支持语言:English

    RuoYi若依管理系统-其他

    邮箱重复验证个人中心刷新后样式问题操作日志返回参数添加非空验证velocity剔除commons-collections版本,防止3.2.1版本的反序列化漏洞子表模板默认日期格式化代码生成预览语言根据后缀名高亮显示代码生成主子表相同...

    Struts in Action中文版

    1. 介绍.........................................................................................................18 1.1. 关于本书..........................................................................

    struts in Action

    1. 介绍.........................................................................................................18 1.1. 关于本书...........................................................................

Global site tag (gtag.js) - Google Analytics