首页 > 解决方案 > 使用 FieldMethodizer 访问静态文件打印“无法为字段方法化添加 x:x”

问题描述

当我尝试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

标签: javaapachevelocity

解决方案


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


推荐阅读