Java Project - Velocity不替换某些字符串

时间:2014-08-21 16:09:20

标签: java templates velocity

我正在加载一个带有速度和chnaging一些参数的vm tgemplate 我有大约10个参数可以在我的模板中更改

对于我的模板的以下部分

window.location = "$html5Dirhtml5/index.html?page=$pageNumber"

我得到了这个输出

window.location = "$html5Dirhtml5/index.html?page=1"

任何想法为什么速度不转换html5Dir属性但它正在设置页面?

属性肯定存在于传递给velocity的属性中,我已经检查过了 我的速度代码如下

public static StringWriter generateTextFromTemplate(String templateFile, Map<String, String> params) {
    LOG.debug("Entered generateTextFromTemplate - templateFile:{}", templateFile);


    StringWriter writer = null;
    try {
        VelocityEngine velocityEngine = new VelocityEngine();
        velocityEngine.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath"); 
        velocityEngine.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName());
        velocityEngine.init();

        VelocityContext velocityContext = new VelocityContext();

        for (Map.Entry<String, String> entry : params.entrySet()) {
            String key = entry.getKey();
            String value = entry.getValue();
            velocityContext.put(key, value);
            LOG.error("key:{}, value:{}", key, value);
        }

        //  get the Template 
        Template template = velocityEngine.getTemplate(templateFile);

        //  now render the template into a Writer 
        writer = new StringWriter();
        template.merge( velocityContext, writer );

    } catch (Exception e) {
       LOG.error("{}", e);
       writer = null;
    }

    return writer;
}

1 个答案:

答案 0 :(得分:1)

首先,检查是否存在实际的html5Dirhtml5参数。如果那不是您想要的参数,而且它是html5Dir,那么您需要在模板中使用$ {html5Dir}形式来划分实际名称。