连字符处理Apache Velocity 2.1 - 这是一个Bug吗?

问题描述 投票:1回答:1

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

我正在使用新发布的Apache Velocity 2.1,我正在测试连字符作为标识符名称但是我遇到了一个bug或什么?我已经设置了属性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 map中的那个不是..它会抛出这样的错误:


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个回答
0
投票

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

您可以使用以下方法解决此问题

$map.get("sample-slash")

要么

$map["sample-slash"]

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

© www.soinside.com 2019 - 2024. All rights reserved.