【问题标题】:Hyphen Handling Apache Velocity 2.1 - Is this a Bug?连字符处理 Apache Velocity 2.1 - 这是一个错误吗?
【发布时间】:2019-04-16 17:35:09
【问题描述】:

我在 Apache Velocity 邮件列表中提出了这个问题,但我没有收到回复,所以我在这里重新发布......

我正在使用新发布的 Apache Velocity 2.1,我正在测试连字符作为标识符名称,但我遇到了错误或什么?我已经设置了 Velocity.PARSER_HYPHEN_ALLOWED 属性,但它只允许用于单个数据,而不是地图或集合数据。

我有这个模板:

hello-world.properties.vm 
---------------------------------------------------------- 
Slash: ${sample-slash}
Slash in a Map: ${map.sample-slash}

我有这个示例测试用例:

public class ApacheVelocityTest {
    private final String RESOURCES_DIR = "src/test/resources";

    @Test
    public void testVelocity() {
        Path templatePath = Paths.get(RESOURCES_DIR, "templates", "hello-world.properties.vm");


        VelocityEngine ve = new VelocityEngine();
        ve.setProperty(Velocity.PARSER_HYPHEN_ALLOWED, true);

        ve.init();
        Template t = ve.getTemplate(templatePath.toString());

        VelocityContext context = new VelocityContext();

        context.put("sample-slash", "SLASH");

        Map<String, String> sampleData = createData();
        context.put("map", sampleData);

        StringWriter writer = new StringWriter();
        t.merge(context, writer);

        System.out.println(writer.toString());
    }

    public Map<String, String> createData() {
        Map<String, String> mapData = new HashMap<String, String>();

        mapData.put("sample-slash", "USER1");

        return mapData;

    }
}

现在,第一个“sample-slash”已正确渲染,但 java 映射中的那个不是..它抛出如下错误:


org.apache.velocity.exception.ParseErrorException: Encountered "-slash}" at src\test\resources\templates\hello-world.properties.vm[line 5, column 22]
Was expecting one of:
    "[" ...
    "|" ...
    "}" ...
    "}" ...

    at org.apache.velocity.Template.process(Template.java:154)

嵌入到 java 映射中的对象正在抛出解析器异常。

我对此开发人员有任何解决方法吗?非常感谢任何指针。

【问题讨论】:

标签: java templates velocity


【解决方案1】:

这是 2.1 版中 parser.allow_hyphen_in_identifiers 向后兼容模式的错误。

您可以使用以下方法解决它:

$map.get("sample-slash")

$map["sample-slash"]

该错误已在主开发分支中修复。

【讨论】:

    猜你喜欢
    • 2012-02-19
    • 2018-03-06
    • 2011-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-16
    • 2012-09-18
    相关资源
    最近更新 更多