本文實例講述了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")!=nullcontextPvd.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程序設計有所幫助。
您可能感興趣的文章:- 詳解使用jquery.i18n.properties 實現web前端國際化
- 基于jQuery.i18n實現web前端的國際化
- java讀取properties配置文件的方法
- Java遍歷Properties所有元素的方法實例
- java獲取properties屬性文件示例
- Java讀取properties配置文件時,出現中文亂碼的解決方法
- 詳解五種方式讓你在java中讀取properties文件內容不再是難題
- Java中Properties的使用詳解
- java遍歷properties文件操作指南
- Java中的幾種讀取properties配置文件的方式
- 詳解使用jQuery.i18n.properties實現js國際化