Damien Gallagher
Damien Gallagher

Reputation: 991

Java Project - Velocity Not replacing certain strings

I am loading up a vm tgemplate with velocity and chnaging some of the parameters I have around 10 parameters that can change in my template

For the following portion of my template

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

I get this output

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

Any idea why velocity is not converting the html5Dir attribute but it is setting the page?

The attributes definitely exist in the attributes passed to velocity, I have checked this My velocity code is as follows

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;
}

Upvotes: 1

Views: 703

Answers (1)

Tassos Bassoukos
Tassos Bassoukos

Reputation: 16142

First of all, check that there is an actual html5Dirhtml5 parameter. If that isn't the parameter you want and it's html5Dir, then you need to use the form ${html5Dir} in the template to demarcate what the actual name is.

Upvotes: 1

Related Questions