Solve the problem that the object properties in the freemarker array cannot be obtained

1. Symptoms

When using Freemarker to write templates, when traversing the List and finding the first letter in the object is capitalized and underlined, an error will be reported

The following has evaluated to null or missing

FTL stack trace ("~" means nesting-related):

It means that the content of ${list.Name} is empty and cannot be read. An error will be reported if the first letter of the attribute name in the array is capitalized or the attribute contains camel case.

Freemarker does not recognize the Java object properties in the list when writing the template

2. Solutions

There are two types of template interface generated by freemarker. The [ObjectWrapper] parameter used in the default method is ObjectWrapper.SIMPLE_WRAPPER, but in our case another type is needed, namely: ObjectWrapper.BEANS_WRAPPER, that is to say, custom scan objects of type JAVABean:

//生成代码文件
FileOutputStream fos = new FileOutputStream(newFile);
Writer out = new BufferedWriter(new OutputStreamWriter(fos, "utf-8"), 10240);
try {
    
    
    //默认freemarker生成模板的方法,无法识别 List中的JAVA对象属性
    //template.process(dataMap, out);
    //修改后的方法
   template.process(dataMap, out, ObjectWrapper.BEANS_WRAPPER);
} catch (Exception e) {
    
    
   e.printStackTrace();
} finally {
    
    
   out.close();
}

Guess you like

Origin blog.csdn.net/qq_35921773/article/details/129106537