Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read properties file using jQuery or JavaScript

I am newbie in jquery. I would like to read Java properties file in my jsp page using javascript or jquery. I'm goggling about it but not satisfied.

My application is developed by Struts2, Eclipse Juno and Windows7. This is my javascript

 function checkedRadioForDelete(f) {
    var chx = document.getElementsByTagName('input');
    for ( var i = 0; i < chx.length; i++) {
        if (chx[i].type == 'radio' && chx[i].checked) {
            var con = confirm("Are you sure to delete?");
            if (con != true) {
            } else {
               f.action = "MyAction.action";
               f.submit();
             }
            return true;
         }
    }
    alert("Please select one.");
    return false;
 }

This is my properties file

msg.confirm=Are you sure to delete?
msg.alert=Please select one.

I would like to read confirm and alert box values from properties file. So is it possible with Ajax or jquery or javascript?

like image 422
Sai Ye Yan Naing Aye Avatar asked Feb 15 '23 11:02

Sai Ye Yan Naing Aye


2 Answers

One shouldn't use scriptlets in JSP, you can easily do it using struts2 tags.

Change your Javascript in JSP as follows :

function checkedRadioForDelete(f) {
    var chx = document.getElementsByTagName('input');
    for ( var i = 0; i < chx.length; i++) {
        if (chx[i].type == 'radio' && chx[i].checked) {
            var con = confirm("<s:text name="msg.confirm"/>");
            if (con != true) {
            } else {
               f.action = "MyAction.action";
               f.submit();
             }
            return true;
         }
    }
    alert("<s:text name="msg.alert"/>");
    return false;
 }
like image 81
coding_idiot Avatar answered Feb 17 '23 01:02

coding_idiot


You can load properties with javascript using messageResource.js library created by me.

1) Include messageResource.js.

<script src="messageResource.min.js"></script>

2) Change javascript as follows.

// initialize messageResource.js  
messageResource.init({
    // path to directory containing properties files
    filePath : 'path/resource'
});

function checkedRadioForDelete(f) {

    // get values from properties files
    var confirmMsg = messageResource.get('msg.confirm', 'fileName');
    var alertMsg = messageResource.get('msg.alert', 'fileName');

    var chx = document.getElementsByTagName('input');
    for ( var i = 0; i < chx.length; i++) {
        if (chx[i].type == 'radio' && chx[i].checked) {
            var con = confirm(confirmMsg);
            if (con != true) {
            } else {
                f.action = "MyAction.action";
                f.submit();
            }
            return true;
        }
    }
    alert(alertMsg);
    return false;
}
like image 43
Khan Avatar answered Feb 17 '23 02:02

Khan