Search code examples
javaapachevelocity

Using FieldMethodizer to access static files prints "Could not add x for field methodizing: x"


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

Solution

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