Ajax Method for reading data from properties resource files, ajaxproperties

Source: Internet
Author: User

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.

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.