Ajax Read Properties resource file data method _ajax related

Source: Internet
Author: User
Tags locale

This article gives an example of how Ajax reads properties resource file data. Share to everyone for your reference. The implementation methods are as follows:

The contents of the properties resource file are as follows:

HELLO=ENGLISHWW
name=english ZK
Emailempty=field cannot be empty!
Emailinvalid=invalid Email address! 

JS invokes Ajax processing code:

$.ajax ({
 type: ' POST ',
 dataType: ' json ',
 URL: '/jeecms/jeecms/ajax/cms/getresourcebundle.do '
 , Async:false,
 success:function (data) {
  jsondata=data.jsi18n;//jsi18n is the name given when Java returns
  Jsi18n=eval_r (' (' +jsondata+ ');//Convert to JSON object
  alert ("property is" +jsi18n.hello);
 },
 error:function (data) {
  Alert ("error");
 }
);

Java Processing file Getresourcebundle.do code:

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);
To judge the language category, ignore
Set keyset=resource_bundle.keyset ();
Read resource file data concatenation into JSON format string returns
string jsonstring = NewString ();
jsonstring+= "{";
for (String key:keyset) {
 jsonstring+= ' "' +key+ '" ' + ": ' + '" ' +resource_bundle.getstring (Key) + ' "' + ',";
}
Assign the string to the return object's jsi18n (here at random)
jsonroot.put ("jsi18n", Jsonstring.substring (0,jsonstring.length ()-1) + "}");
return SUCCESS;
}

Note: JS request succeeds, if other JS also want to use to read out content, then assign the return value to a global variable.

I hope this article will help you with your AJAX programming.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.