velocity - Apache Velocity GenericTools ToolsManager 不工作
问题描述
我正在使用 Apache Velocity 模板引擎仅从字符串呈现。我已经使它与下面的代码一起工作,但 ToolManager 似乎没有工作。通过以下设置,我无法使用此处定义的任何默认工具https://github.com/apache/velocity-tools/blob/8e7f20a4/velocity-tools-generic/src/main/resources/org/apache/velocity/ tools/generic/tools.xml
例如,来自 DateTool 的 $date 不起作用。
@Service
public class VelocityServiceImpl implements VelocityService {
private final VelocityEngine velocityEngine;
private final ToolContext toolContext;
public VelocityServiceImpl() {
Properties properties = new Properties();
properties.setProperty("resource.loader", "class");
properties.setProperty("class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
properties.setProperty(RuntimeConstants.EVENTHANDLER_INCLUDE, IncludeRelativePath.class.getName());
this.velocityEngine = new VelocityEngine();
this.velocityEngine.init(properties);
ToolManager manager = new ToolManager();
manager.setVelocityEngine(this.velocityEngine);
this.toolContext = manager.createContext();
}
@Override
public String getTemplateFromString(String template, Map<String, Object> context) throws IOException {
if (template == null) {
template = "";
}
Writer writer = new StringWriter();
VelocityContext velocityContext = new VelocityContext(context, this.toolContext);
this.velocityEngine.evaluate(velocityContext, writer, "getTemplateFromString: ", template);
writer.flush();
return writer.toString();
}
}
我的依赖:
implementation group: 'org.apache.velocity', name: 'velocity', version: '1.7'
implementation group: 'org.apache.velocity', name: 'velocity-engine-core', version: '2.3'
implementation group: 'org.apache.velocity.tools', name: 'velocity-tools-generic', version: '3.1'
implementation group: 'commons-beanutils', name: 'commons-beanutils', version: '1.9.4'
有什么我想念的想法吗?
解决方案
推荐阅读
- google-cloud-platform - Google Cloud VM 启动脚本无法安装 pip3
- python - 将循环代码转换为python中的一行代码?
- tensorflow - 为什么我不能通过 google colab 将 TensorFlow 预测图像从谷歌地球引擎写入谷歌云存储桶?
- reactjs - ReactJS Formik 以红色显示错误消息
- python - 在 Python 中识别子字符串(沿着字符串中的索引移动) - 只能将 str(不是“int”)连接到 str
- git - Git拉和推不起作用
- vue.js - 可拖动 | 将参数传递给 :move
- cs50 - 我不断收到分段错误,我不知道为什么。我可能错误地使用 strcasecmp 函数,但我不确定
- swift - 如何在导航栏(Xcode)中将右栏按钮项放在彼此之上?
- amazon-web-services - 如何限制对 REST API 的访问以仅匹配用户的 ID?