Ajax Method for reading data from properties resource files, ajaxproperties
This document describes how to use ajax to read data from properties resources. Share it with you for your reference. The specific implementation method is as follows:
The content of the properties resource file is as follows:
hello=englishwwname=english zkemailEmpty=Field cannot be empty!emailInvalid=Invalid email address!
Js calls ajax processing code:
$. Ajax ({type: 'post', dataType: 'json', url: '/jeecms/ajax/cms/getResourceBundle. do ', async: false, success: function (data) {jsonData = data. jsI18n; // jsI18n is the name jsi18n = eval_r ('+ jsonData +') 'given when java returns ')'); // 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 (contextvec. getSessionAttr ("gLanguage ")! = Null & contextvec. getSessionAttr ("gLanguage "). equals ("1") {RESOURCE_BUNDLE = ResourceBundle. getBundle ("jsI18n", Locale. ENGLISH);} else {RESOURCE_BUNDLE = ResourceBundle. getBundle ("jsI18n", Locale. CHINA);} // identifies the language class, ignoring Set keySet = RESOURCE_BUNDLE.keySet (); // returns String jsonString = newString () after splicing data from the resource file into a json String (); jsonString + = "{"; for (String key: keySet) {jsonString + = '"' + key + '"' + ":" + '"' + RESOURCE_BUNDLE.getString (key) + '"' +", ";}// assign the string to the jsI18n (random here) jsonRoot of the returned object. put ("jsI18n", jsonString. substring (0, jsonString. length ()-1) + "}"); return SUCCESS ;}
Note: after a successful js request, if you want to use the read content in other js, the return value is assigned to a global variable.
I hope this article will help you with Ajax programming.