提问者:小点点

自由标记 以下内容已评估为空或缺失


我在自由标记的评估过程中遇到以下错误。然而,此错误仅在构建中显示,而不在IDE中显示。

{“code”:“NoApplicableCode”,“description”:“处理模板freemarkTest.ftl时出错\n以下内容的计算结果为null或缺失:\n==

//freemarker function
<#assign keywordsJSON = "${loadJSON('path/to/file/random.json')}">
//function for creating freemarker function
protected void addUtilityFunctions(String baseURL, Map<String, Object> model) {
    model.put("loadJSON", parseJSON());
}

private TemplateMethodModelEx parseJSON() {
    return arguments -> loadJSON(arguments.get(0).toString());
}

private String loadJSON(String filePath) {
    JSONParser parser = new JSONParser();
    try {
            File file = fileFinder.findFile(filePath);
            if (file == null) {
                LOGGER.warning("File is outside of data directory");
                throw new RuntimeException(
                        "File " + filePath + " is outside of the data directory");
            }
            return parser.parse(new FileReader(file.getPath())).toString();
        } catch (Exception e) {
            LOGGER.warning("Failed to parse JSON file " + e.getLocalizedMessage());
        }
        LOGGER.warning("Failed to create a JSON object");
        return "Failed to create a JSON object";
    }

共1个答案

匿名用户

问题是不同的freemarker模板有不同的功能。我没有把函数放在正确的位置。问题中所有的语法和函数调用都是真的。