非web环境Velocity tool的使用

功能:使用Velocity的tool功能,去格式化日期字段,

条件限制:非web环境,velocity1.4.jar + velocity-tool-view-1.1.jar

思路:模拟web环境下的ChainedContext,来定制一个context即可


ExpandedBlockStart.gif View Code
public   class  VelocityUtil
{
    
public   static   final  String ENCODING  =   " UTF-8 " ;
    
    
public   static   final  String TOOLBOX_PATH  =   " velocity/template/toolbox.xml " ;
    
    
private  VelocityUtil(){};

    
private   static  VelocityContext getContext(Map < String, Object >  params)  throws  Exception
    {
        ToolboxContext toolboxContext 
=   new  ToolboxContext(params);
        XMLToolboxManager xmlManager 
=   new  XMLToolboxManager();
        String toolboxFile 
=  VelocityUtil. class .getClassLoader().getResource(TOOLBOX_PATH).getPath();
        xmlManager.load(
new  FileInputStream( new  File(toolboxFile)));
        ChainedContext context 
=   new  ChainedContext(toolboxContext);
        context.setToolbox(xmlManager.getToolboxContext(context));
        
return  context;
    }
    
    
private   static  VelocityEngine init(String templatePath)  throws  Exception
    {
        VelocityEngine ve 
=   new  VelocityEngine();
        Properties prop 
=   new  Properties();
        prop.setProperty(
" file.resource.loader.path " , VelocityUtil. class .getClassLoader().getResource(templatePath).getPath());
        prop.setProperty(
" input.encoding " , ENCODING);
        prop.setProperty(
" output.encoding " , ENCODING);
        ve.init(prop);
        
return  ve;
    }
    
/**
     * generate string from template
     * 
@param  templatePath    : template files' directionary, example as : velocity/template/hp/execforum
     * 
@param  fileName        : template file's name, suce as : 
     * 
@param  params
     * 
@return
     * 
@throws  Exception
     
*/
    
public   static  String generate(String templatePath, String fileName, Map < String, Object >  params)  throws  Exception
    {
        VelocityEngine ve 
=  init(templatePath);
        Template template 
=  ve.getTemplate(fileName, ENCODING);
        VelocityContext context 
=  getContext(params);
        StringWriter writer 
=   new  StringWriter();
        template.merge(context, writer);
        
// System.out.println("----------\n" + writer.toString());
         return  writer.toString();
    }
    
    
private   static   class  ChainedContext  extends  VelocityContext
    {
        
private   static   final   long  serialVersionUID  =   5729583723890287479L ;
        
private  ToolboxContext toolboxContext  =   null ;

        
public  ChainedContext(Context ctx)
        {
            
super ( null , ctx );
        }
        
public   void  setToolbox(ToolboxContext box)
        {
            toolboxContext 
=  box;
        }
        
public  Object internalGet( String key )
        {
            Object o 
=   null ;
            
if  (toolboxContext  !=   null )
            {
                o 
=  toolboxContext.get(key);
                
if  (o  !=   null )
                {
                    
return  o;
                }
            }
            o 
=   super .internalGet(key);
            
return  o;
        }
    }
}

 

然后在.vm模板中,使用下面的方式来格式化日期,

 

$date.format('h:mm a', $item.startDatetime)
输出: 10:10 下午

 

如果要指定日期的locale,可以继承DateTool,override父类的getLocale方法(例如返回:Locale.US),最后将该类注册到 toolbox.xml即可

输出:10:10 PM

 

 

 

 

转载于:https://www.cnblogs.com/java-koma/archive/2011/04/12/2013666.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值