非web环境Velocity tool的使用

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

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

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


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

 

 

 

 

posted on 2011-04-12 15:02  TroyZ  阅读(689)  评论(0编辑  收藏  举报