Velocity中加载vm文件的三种方式(也可以参考demo文件夹下的例子对照)
Velocity中加载vm文件的三种方式:
方式一:加载classpath目录下的vm文件
Properties p = new Properties();
p.put("file.resource.loader.class",
"org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
Velocity.init(p);
...
Velocity.getTemplate(templateFile);
方式二:根据绝对路径加载,vm文件置于硬盘某分区中,如:d:\\tree.vm
Properties p = new Properties();
p.setProperty(VelocityEngine.FILE_RESOURCE_LOADER_PATH, "d:\\");
Velocity.init(p);
...
Velocity.getTemplate("tree.vm");
方式三:使用文本文件,如:velocity.properties,配置如下:
input.encoding = UTF-8
file.resource.loader.class=org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
再利用如下方式进行加载
Properties p = new Properties();
p.load(this.getClass().getResourceAsStream("/velocity.properties"));
Velocity.init(p);
...
Velocity.getTemplate(templateFile);
方法四:
org.apache.velocity.runtime.resource.loader.URLResourceLoader
2.加载远程文件的问题
解决方案1.
http://mail-archives.apache.org/mod_mbox/velocity-user/200211.mbox/%3CB9F95F44.1AB00%25zodiac@holoweb.net%3E<!--[if !supportNestedAnchors]--><!--[endif]-->
解决方案2
http://badqiu.javaeye.com/blog/691617
http://code.google.com/p/rapid-framework/source/browse/trunk/rapid-framework/src/rapid_framework_common/cn/org/rapid_framework/web/httpinclude/HttpInclude.java
public class
ApplicationContext extends AbstractInterceptor{
private static final long serialVersionUID =
5728757263270691902L;
private static Logger log =
Logger.getLogger(ApplicationContext.class);
@Override
public String intercept(ActionInvocation ai)
throws Exception {
log.info("Oh,yeah.
I'm intercepting.");
try{
Map<String, Object> application =
ai.getInvocationContext().getApplication();
Object ac =
application.get("ac");
Object httpInclude =
application.get("httpInclude");
log.info(application.get("httpInclude"));
if(null==ac && null!=application)
application.put("ac",
new AgencyConstants());
if(null==httpInclude&&null!=application)
application.put("httpInclude",new-HttpInclude(ServletActionContext.getRequest(),
ServletActionContext.getResponse()));
if(null==application)
return
Action.ERROR;
return ai.invoke();
}catch(Exception
e){
log.error(e.getLocalizedMessage());
e.printStackTrace();
return Action.ERROR;
}
}
}
参考:
http://velocity.apache.org/engine/releases/velocity-1.5/developer-guide.html
http://hailinhe1986-163-com.javaeye.com/blog/657211
Other Demo
http://www.javaranch.com/journal/2004/03/Velocity-AnIntroduction.html
http://www.suneca.com/article.asp?id=22
分享到:
相关推荐
velocity是一款除JSP以外的另一款模板引擎,也许很多地方比常规的JSP更加好,或者说它这种模板引擎是基于常规JSP与一些标签整合后。 zip包中有两个jar包,提供使用。
Velocity模板(VM)语言介绍,简单的介绍,方便入门!
velocity中文文档 教程 velocity中文文档 教程 velocity中文文档 教程
Jsp结合Velocity实现依据Word模板文件生成对应数据文件,操作非常灵活,方便。
Velocity中文指南
maven SpringMVC velocity。itext 按照 velocity 生成pdf 并下载。
// 按文件加载 // ve.init(); // Template t = ve.getTemplate("src/velocity/hellovelocity.vm"); ve.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath"); // 在类路径下加载 ve.setProperty(...
spring+velocity+ajax带进度条上传文件,上传显示文件的状态。
velocity中文手册 velocity学习资料
velocity精典中文帮助文档
什么是Velocity? Velocity是基于Java的模板引擎。它允许Web页面开发者引用Java代码中定义的方法。...Velocity将Java代码从Web页面中分离,使Web站点更具长期可维护性,并提供了一种替代JSP或PHP的
eclipse中velocity插件
使用 Velocity 可以在文本文件里面生成邮件内容,而不是在 Java 代码中拼接字符串。 4. 转换 xml:Velocity 提供一个叫 Anakia 的 ant 任务,可以读取 XML 文件并让它能够被 Velocity 模板读取。一个比较普遍的应用...
eclispe的velocity插件,是vm文件绝好的编辑器。
赠送jar包:velocity-1.7.jar; 赠送原API文档:velocity-1.7-javadoc.jar; 赠送源代码:velocity-1.7-sources.jar; 包含翻译后的API文档:velocity-1.7-javadoc-API文档-中文(简体)版.zip 对应Maven信息:...
velocity中文指南
Velocity模板引擎Velocity模板引擎Velocity模板引擎Velocity模板引擎Velocity模板引擎Velocity模板引擎Velocity模板引擎Velocity模板引擎Velocity模板引擎Velocity模板引擎
简单的验证工具,用于检测Apache Velocity模板中的错误。 它遍历目录并验证每个.vm文件。 错误会显示文件名,错误和可能的解决方案。 用法 java -jar velocityvalidator.jar path_to_templates [-rules=path_to_...
Velocity是一个基于java的模板引擎(template engine)。它允许任何人仅仅简单的...Velocity将java代码从web页面中分离出来,这样为web站点的长期维护提供了便利,同时也为我们在JSP和PHP之外又提供了一种可选的方案。