微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Apache Velocity GenericTools ToolsManager 不工作

如何解决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'

知道我缺少什么吗?

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。