使用 FieldMethodizer 访问静态文件打印 "Could not add x for field methodizing: x"

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

当我尝试在我的模板中使用 velocity 的 FieldMethodizer 变量时,它打印错误。

我正在使用 SparkJava 框架和速度模板引擎。

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

我收到错误

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

使用 FieldMethodizer

时,您需要将 全 class 名称 添加到模型中作为 com.package.Path

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

然后在模板中使用$WebPath.Web

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

$runtime.COUNTER_NAME

我将 model.put 行更改为 model.put("WebPath", new FieldMethodizer(new Path.Web())); 来解决这个问题。