这篇文章主要介绍了ajax读取properties资源文件数据的方法,实例分析了基于ajax实现读取properties资源文件数据的相关技巧,需要的朋友可以参考下
本文实例讲述了ajax读取properties资源文件数据的方法。分享给大家供大家参考。具体实现方法如下:
properties资源文件的内容如下:
hello=englishww
name=english zk
emailempty=field cannot be empty!
emailinvalid=invalid email address!
js调用ajax处理代码:
$.ajax({
type:'post',
datatype:'json',
url:'/jeecms/jeecms/ajax/cms/getresourcebundle.do',
async:false,
success:function(data){
jsondata=data.jsi18n;//jsi18n是java返回时赋予的名称
jsi18n=eval_r('('+jsondata+')');//转化为json对象
alert("property is "+jsi18n.hello);
},
error:function(data){
alert("error");
}
});
java处理文件getresourcebundle.do代码:
publicstring getresourcebundle(){
resourcebundle resource_bundle;
if(contextpvd.getsessionattr("glanguage")!=null&&contextpvd.getsessionattr("glanguage").equals("1")){
resource_bundle=resourcebundle.getbundle("jsi18n",locale.english);
}else{
resource_bundle =resourcebundle.getbundle("jsi18n",locale.china);
}//判断语言类别的,忽视
set keyset=resource_bundle.keyset();
//读取资源文件数据拼接成json格式字符串返回
string jsonstring = newstring();
jsonstring+="{";
for(string key:keyset){
jsonstring+='"'+key+'"'+":"+'"'+resource_bundle.getstring(key)+'"'+",";
}
//把字符串赋给返回对象的jsi18n(这里随意)
jsonroot.put("jsi18n",jsonstring.substring(0,jsonstring.length()-1)+"}");
return success;
}
上面是我整理给大家的,希望今后会对大家有帮助。
相关文章:
各种ajax方法的使用比较详解
利用h5特性formdata实现不刷新文件上传
基于h5 ajax实现手机定位
以上就是ajax读取properties资源文件数据的方法的详细内容。