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();
 }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值