使用 jQuery 或 JavaScript 读取属性文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20416185/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Read properties file using jQuery or JavaScript
提问by Sai Ye Yan Naing Aye
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.
我是 jquery 的新手。我想使用 javascript 或 jquery 在我的 jsp 页面中读取 Java 属性文件。我盯着它看,但不满意。
My application is developed by Struts2, Eclipse Juno and Windows7. This is my javascript
我的应用程序是由 Struts2、Eclipse Juno 和 Windows7 开发的。这是我的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 confirmand alertbox values from properties file. So is it possible with Ajax or jquery or javascript?
我想从属性文件中读取确认和警报框值。那么可以使用 Ajax 或 jquery 或 javascript 吗?
采纳答案by coding_idiot
One shouldn't use scriptlets in JSP, you can easily do it using struts2 tags.
不应在 JSP 中使用 scriptlet,您可以使用 struts2 标签轻松实现。
Change your Javascript in JSP as follows :
在 JSP 中更改您的 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("<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;
}
回答by jsjunkie
Note: Updating my answer based on comments.
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;
}
回答by Khan
You can load properties with javascript using messageResource.jslibrary created by me.
您可以使用我创建的messageResource.js库使用 javascript 加载属性。
1) Include messageResource.js.
1) 包含 messageResource.js。
<script src="messageResource.min.js"></script>
2) Change javascript as follows.
2)按如下方式更改javascript。
// 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;
}
回答by Keshan Fernando
If anyone needs help for a Web Application, please see my answerthat 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.
如果有人需要有关 Web 应用程序的帮助,请参阅我的回答,其中解释了从项目目录中的属性文件读取属性的最简单方法。总之,您将能够在隐藏输入中填充一些选定的属性,然后从 JavaScript 读取隐藏的输入值。