某业务系统采用freemark根据模板生产文件的时候发现在同一个线程内,同一个数据源“连续”生成两次文件结果不一样。
JDK1.6 spring-framework-3.0.3 freemarker-v2.3.20
同一个数据源“连续”生成两次文件结果不一样。
public static void main(String[] args) {
String templateFilePath = "H:\\temp\\javaBean\\template.txt";
String saveFilePath = "H:\\temp\\javaBean\\result.txt";
Map dataMap = new HashMap();
// 模拟数据库获取数据
Map testTarget = new LinkedCaseInsensitiveMap();
testTarget.put("key", "数据库数据。");
// 业务逻辑
String dealKey = "KEY";
if(testTarget.containsKey(dealKey)){
testTarget.put(dealKey, "新的业务逻辑值。");
}
dataMap.put("map", testTarget);
// 下面根据dataMap template 生成渲染后的模板
try {
HelperFreemarker.createFile(dataMap, templateFilePath, saveFilePath);
HelperFreemarker.createFile(dataMap, templateFilePath, saveFilePath+".second");
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("========");
}
template.txt 内容为
AAAA
${map.key}
BBBB
result.txt 第一次结果:
AAAA
新的业务逻辑值。
BBBB
result.txt.second 第二次结果:
AAAA
数据库数据。
BBBB
LinkedCaseInsensitiveMap 结构为 mybatis读取数据库后生成的默认Map
连续生成两次结果不一样。
HelperFreemarker.createFile(dataMap, templateFilePath, saveFilePath);
HelperFreemarker.createFile(dataMap, templateFilePath, saveFilePath+".second");
put方法 、get方法:
@Override
public V put(String key, V value) {
this.caseInsensitiveKeys.put(convertKey(key), key);
return super.put(key, value);
}
@Override
public boolean containsKey(Object key) {
return (key instanceof String && this.caseInsensitiveKeys.containsKey(convertKey((String) key)));
}
@Override
public V get(Object key) {
if (key instanceof String) {
return super.get(this.caseInsensitiveKeys.get(convertKey((String) key)));
}
else {
return null;
}
}
可以看到实现大小写不敏感的方法是用
private final Map
这个HashMap来保存大小写的对应关系。这里当put两个大小写不同key的时候,只能取出最后一个key的值。
2. 跟踪freemark执行代码
java/freemarker/template/SimpleHash 里面get方法
public TemplateModel get(String key) throws TemplateModelException {
Object result = map.get(key);
// The key to use for putting -- it is the key that already exists in
// the map (either key or charKey below). This way, we'll never put a
// new key in the map, avoiding spurious ConcurrentModificationException
// from another thread iterating over the map, see bug #1939742 in
// SourceForge tracker.
final Object putKey;
if (result == null) {
if(key.length() == 1) {
// just check for Character key if this is a single-character string
Character charKey = new Character(key.charAt(0));
result = map.get(charKey);
if (result == null && !(map.containsKey(key) || map.containsKey(charKey))) {
return null;
}
else {
putKey = charKey;
}
}
else if(!map.containsKey(key)) {
return null;
}
else {
putKey = key;
}
}
else {
putKey = key;
}
if (result instanceof TemplateModel) {
return (TemplateModel) result;
}
TemplateModel tm = wrap(result);
if (!putFailed) {
try {
map.put(putKey, tm);
} catch (Exception e) {
// If it's immutable or something, we just keep going.
putFailed = true;
}
}
return tm;
}
上面get的过程,对 map的值 为了
// The key to use for putting -- it is the key that already exists in
// the map (either key or charKey below). This way, we'll never put a
// new key in the map, avoiding spurious ConcurrentModificationException
// from another thread iterating over the map, see bug #1939742 in
// SourceForge tracker.
对值进行put,put的时候导致只能取出 小写 key的值,原来是大写的KEY