非web环境Velocity tool的使用
功能:使用Velocity的tool功能,去格式化日期字段,
条件限制:非web环境,velocity1.4.jar + velocity-tool-view-1.1.jar
思路:模拟web环境下的ChainedContext,来定制一个context即可

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;
}
}
}
{
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
· .NET制作智能桌面机器人:结合BotSharp智能体框架开发语音交互
· 软件产品开发中常见的10个问题及处理方法
· .NET 原生驾驭 AI 新基建实战系列:向量数据库的应用与畅想
· 从问题排查到源码分析:ActiveMQ消费端频繁日志刷屏的秘密
· 一次Java后端服务间歇性响应慢的问题排查记录
· 互联网不景气了那就玩玩嵌入式吧,用纯.NET开发并制作一个智能桌面机器人(四):结合BotSharp
· 一个基于 .NET 开源免费的异地组网和内网穿透工具
· 《HelloGitHub》第 108 期
· Windows桌面应用自动更新解决方案SharpUpdater5发布
· 我的家庭实验室服务器集群硬件清单