Apache的Velocity - getTemplate()。其实它允许传递.vm文件名,我可以在这里传递字符串/对象吗?有什么方法可以传递字符串/对象吗?Apache的Velocity - getTemplate()。如何传递字符串/对象而不是.VM文件
回答
看看我搜索了在同一个问题小时StringResourceLoader
我发现一个StringResourceLoader的例子。 http://velocity.apache.org/engine/devel/apidocs/org/apache/velocity/runtime/resource/loader/StringResourceLoader.html。但是我们正在将字符串formmate中的模板信息。我们需要传递字符串而不是.vm。我应该为这种情况做些什么。请如果有任何示例代码。 – vasantharajan
StringResourceLoader允许您直接将模板添加到存储库,然后像其他任何模板一样检索它们。 StringResourceLoader.getRepository()。putStringResource(myTemplateName,myTemplateString); –
,终于找到了单元测试代码,显示一切必要的。
尝试过所有测试,并且所有测试都失败了,速度为1.7! –
这是为我工作的一个示例代码。
速度版本:1.7
我使用log4j作为记录器。
import org.apache.log4j.Logger;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.runtime.RuntimeConstants;
import org.apache.velocity.runtime.resource.loader.StringResourceLoader;
import org.apache.velocity.runtime.resource.util.StringResourceRepository;
private static void velocityWithStringTemplateExample() {
// Initialize the engine.
VelocityEngine engine = new VelocityEngine();
engine.setProperty(RuntimeConstants.RUNTIME_LOG_LOGSYSTEM_CLASS, "org.apache.velocity.runtime.log.Log4JLogChute");
engine.setProperty("runtime.log.logsystem.log4j.logger", LOGGER.getName());
engine.setProperty(Velocity.RESOURCE_LOADER, "string");
engine.addProperty("string.resource.loader.class", StringResourceLoader.class.getName());
engine.addProperty("string.resource.loader.repository.static", "false");
// engine.addProperty("string.resource.loader.modificationCheckInterval", "1");
engine.init();
// Initialize my template repository. You can replace the "Hello $w" with your String.
StringResourceRepository repo = (StringResourceRepository) engine.getApplicationAttribute(StringResourceLoader.REPOSITORY_NAME_DEFAULT);
repo.putStringResource("woogie2", "Hello $w");
// Set parameters for my template.
VelocityContext context = new VelocityContext();
context.put("w", "world!");
// Get and merge the template with my parameters.
Template template = engine.getTemplate("woogie2");
StringWriter writer = new StringWriter();
template.merge(context, writer);
// Show the result.
System.out.println(writer.toString());
}
- 1. Apache的速度 - 是getTemplate()调用需要一个字符串不是文件名?
- 2. Postgres JSON函数传递的字符串,而不是对象
- 3. 隔离作用域'='传递字符串而不是对象
- 4. 如何通过java脚本将参数从另一个vm文件(velocity)传递给vm文件(Velocity)?
- 5. 如何在JS中的对象文字中传递字符串?
- 6. JSCH - 传递私钥字符串而不是文件路径
- 7. 如何从apache velocity * .vm文件调用javascript函数?
- 8. Apache Velocity ::如何从.vm文件获取整个HTML
- 9. Apache配置添加对象而不是字符串
- 10. 带有文件对象而不是字符串的SafeConfigParser.read()
- 11. PyQt4.QtCore.QVariant对象而不是字符串?
- 12. 显示对象而不是字符串
- 13. 如何将字符串传递给批量插入而不是文件?
- 14. 如何在android sqlite中传递字符串而不是字符串参数
- 15. 将null传递给喜欢使用字符串的方法,而不是对象
- 16. Grails配置文件返回空对象,而不是字符串
- 17. 使用Apache HTTP客户端而不将字符串作为字符串传递?
- 18. 使用java检查Apache Velocity(.vm文件)中的文本
- 19. 如何使symfony2请求对象将UploadedFile对象传递给实体类而不是字符串
- 20. 传递一个列名作为对象,而不是一个字符串data.table
- 21. 如何使用只传递一个字符串[],而不是一个对象的jQuery.tmpl()插件
- 22. 将字符串传递到对象javascript
- 23. 将对象字符串的值传递给字符串
- 24. Laravel邮件:传递字符串而不是视图
- 25. 是否可以在不提供输入文件的情况下调用xsl Apache FOP,而是传递字符串
- 26. 笨传递数组,而不是对象
- 27. Apache Velocity - 根据总的Foreach计数分析VM文件
- 28. 如何将GtkComboBox用于对象,而不是字符串?
- 29. AngularJS如何返回字符串而不是Json对象
- 30. 如何将字符串传递给Python中的对象类
的可能的复制[如何使用String作为Velocity模板?](http://stackoverflow.com/questions/1432468/how-to-use-string-as-velocity-template) – Toparvion