本文实例讲述了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;
}
注意:js请求成功后,如果其它js里也要用到读出的内容,则把返回值赋给一个全局变量。
希望本文所述对大家的Ajax程序设计有所帮助。
本文地址:https://www.stayed.cn/item/1420
转载请注明出处。
本站部分内容来源于网络,如侵犯到您的权益,请 联系我