skip to Main Content

When I try to use velocity’s FieldMethodizer variable in my template it prints error.

I’m using SparkJava framework and velocity template engine.

    public static String render(Map<String, Object> model, String templatePath) {
        model.put("WebPath", new FieldMethodizer("Path.Web"));
        return strictVelocityEngine().render(new ModelAndView(model, templatePath));
    }

    private static VelocityTemplateEngine strictVelocityEngine() {
        VelocityEngine configuredEngine = new VelocityEngine();
        configuredEngine.setProperty("runtime.references.strict", true);
        configuredEngine.setProperty("resource.loader", "class");
        configuredEngine.setProperty("class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
        return new VelocityTemplateEngine(configuredEngine);
    }

I get error

Could not add Path.Web for field methodizing: Path.Web

2

Answers


  1. Chosen as BEST ANSWER

    I changed model.put line to model.put("WebPath", new FieldMethodizer(new Path.Web())); to resolve this.


  2. You need to add to model the full class name as com.package.Path when using FieldMethodizer

    context.put("runtime", new FieldMethodizer( "org.apache.velocity.runtime.Runtime" ));

    And then use in template $WebPath.Web

    and then in your template, you can access any of your static fields in this way :

    $runtime.COUNTER_NAME
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search