我的页面上有一个 jstree 树结构,它使用 JSON 数据和 AJAX。有一些斯堪的纳维亚字符(ä 和 ö)处理不当。
Jstree 通过 Java servlet 过滤器获取 JSON 结构。该结构被编码为 UTF-8。当我用 firebug 查看返回的 JSON 结构时,斯堪的纳维亚字符显示正确。我尝试将字符编码更改为 ISO 8859-4 只是为了看看它是否有帮助,但它没有。
我不确定代码的哪些部分与此问题相关,但这里有一些部分。
初始化树:
.jstree({
"json_data" : {
"ajax" : {
"url" : hostUrl+"/json/getAreaTree?treeType=Areas",
"data" : function (n) {
return { id : n.attr ? n.attr("id") : 0 };
}
}
},
一些servlet过滤器代码:
protected class GetAreaTreeContext extends ActionContext implements StreamResponseContext{
private byte[] bytes;
public GetAreaTreeContext() {
super("getAreaTree");
}
@Override
public byte[] getBytes() {
return this.bytes;
}
@Override
public String getContentType() {
return "application/json; charset=UTF-8";
}
@Override
protected boolean doAction() {
if (!getWebSessionObject().isValid())
return false;
Map<String,Object> p = getParameterMap();
String type = (String)p.get("treeType");
String id = (String)p.get("id");
if(id.equals("1") || id.equals("0") || id.equals("id1") || id.equals("id0")){ //get the tree only if request comes from initial situation (id=0) or the root (id=1)
try {
this.bytes = ObjectFactory.getInstance().getDbManager().getAreaFolderTree(type, phone).getBytes();
} catch (Exception ex) {
this.result = "";
}
return bytes.length > 0;
}else{
//init the array again so that when empty folders make ajax requests, they dont get the tree
this.bytes = new byte[0];
return true;
}
}
}
如何让 jstree JSON_DATA 插件处理 UTF-8 编码的斯堪的纳维亚字符?