非web环境Velocity tool的使用
2011-04-12 15:02
267 查看
功能:使用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
条件限制:非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
相关文章推荐
- 【jeecg Docker安装】使用 Docker 搭建 Java Web 运行环境
- 手把手教你在centos下编译安装配置高性能web服务器环境nginx+php(fast-cgi)+mysql等/适合小内存vps使用
- 使用webpack && react环境
- $velocityCount和Tool的使用
- 使用webpack搭建react开发环境的方法
- 在 Lotus Quickr for Domino 环境中使用 Tivoli Access Manager WebSEAL 作为反向代理服务器
- 使用webpack搭建vue开发环境
- 在多线程环境下使用HttpWebRequest或者调用Web Service(连接报超时问题)
- 在React+Babel+Webpack环境中使用ESLint
- Eclipse中使用Maven配置WebDriver+TestNg自动化测试环境(一)
- WebPack安装-使用-Loader-配置-插件-环境-排错详解(下扩展)
- 从零开始使用Docker构建Java Web开发运行环境
- 怎么样使用webserver stress tool工具呢?
- 转:使用 Docker 搭建 Java Web 运行环境
- 使用 Docker 搭建 Java Web 运行环境
- Microsoft Web Application Stress Tool无法使用问题
- 使用 Docker 搭建 Java Web 运行环境
- Microsoft Web Application Stress Tool 使用
- Velocity笔记--使用Velocity获取动态Web项目名的问题
- Linux环境下VMware使用之实用杀进程tool