用户登录
用户注册

分享至

ajax读取properties资源文件数据的方法

  • 作者: 会玩段子的小熊
  • 来源: 51数据库
  • 2021-08-24

本文实例讲述了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程序设计有所帮助。

软件
前端设计
程序设计
Java相关