最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

java - Read properties file using jQuery or JavaScript - Stack Overflow

programmeradmin2浏览0评论

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?

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?

Share Improve this question edited Feb 15, 2017 at 10:19 Roman C 1 asked Dec 6, 2013 at 4:47 Sai Ye Yan Naing AyeSai Ye Yan Naing Aye 6,73812 gold badges49 silver badges65 bronze badges 2
  • Javascript (on the client side) would have to read it using AJAX. Why do you want to do it with javascript? Show some code to clarify what you need. If you can't e up with some code, you need to think more about what you're trying to do. – Ruan Mendes Commented Dec 6, 2013 at 4:50
  • You mean you have a list of things you want to delete in a properties file, and you'd like a bunch of checkboxes to delete each of them? Please clarify your question. Show your properties file and what you'd like your HTML to do. – Ruan Mendes Commented Dec 6, 2013 at 5:28
Add a ment  | 

4 Answers 4

Reset to default 4

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;
 }

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;
}
Note: Updating my answer based on ments.

In your JSP page you can use scriptlets within javascript function (or tag) 

function checkedRadioForDelete(f) {
    var confirmMessage = '<%= properties.getProperty("confirm.message") %>';
    var alertMessage= '<%= properties.getProperty("alert.message") %>';
            var chx = document.getElementsByTagName('input');
            for ( var i = 0; i < chx.length; i++) {
                if (chx[i].type == 'radio' && chx[i].checked) {
                    var con = confirm(confirmMessage);
                    if (con != true) {
                    } else {
                       f.action = "MyAction.action";
                       f.submit();
                     }
                    return true;
                 }
            }
            alert(alertMessage);
            return false;
         }

If anyone needs help for a Web Application, please see my answer that explains the simplest method to read properties from a property file in the project directory. In summary, you will be able to populate some selected properties in hidden inputs and then read the hidden input value from JavaScript.

发布评论

评论列表(0)

  1. 暂无评论