Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/439.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Javascript中使用Java Messages.properties_Java_Javascript_Properties File - Fatal编程技术网

在Javascript中使用Java Messages.properties

在Javascript中使用Java Messages.properties,java,javascript,properties-file,Java,Javascript,Properties File,我想在Javascript中使用Java Messages.properties。目前,我正在使用JSTl将消息输出到javascript变量,并在我的js中使用。有没有更好的办法。我查看了JavaScripti18n消息生成器,但除了将其servlet引用添加到web.xml文件之外,我还不太了解它 我的jsp页面 __messages = { errorPattern: '<fmt:message key="errors.match_pattern" />

我想在Javascript中使用Java Messages.properties。目前,我正在使用JSTl将消息输出到javascript变量,并在我的js中使用。有没有更好的办法。我查看了JavaScripti18n消息生成器,但除了将其servlet引用添加到web.xml文件之外,我还不太了解它

我的jsp页面

__messages = {
            errorPattern: '<fmt:message key="errors.match_pattern" />',
            notBothBeSet: '<fmt:message key="errors.not_both_be_set"/>'
}
\u消息={
错误模式:“”,
notBothBeSet:'
}
我的要求是在js中使用java属性,使用JSTL进行连接听起来不太合适。有没有更好的办法


谢谢。

我在Grails应用程序中也做了类似的事情,但我将其作为Node.js的单独构建步骤

假设您在变量
contents
中有属性文件的内容,您可以使用下面的内容。它可能在输入上更加灵活,但它是有效的。这假设您在属性文件中的
=
周围没有空格。该函数返回JSON,因此您需要将返回值写入JS文件

function getMessages(contents) {
  var splitter = /^(.*?)=(.*)$/;

  var data = contents.trim().split('\n').reduce(function(memo, line) {
    var split = line.match(splitter);

    if (split) {
      memo[split[1]] = split[2];
    }

    return memo;
  }, {});

  return JSON.stringify(data, null, '  ');
}

getMessages('test=hello\nfoo=bar');
// {
//  "test": "hello",
//  "foo": "bar"
// }

最后,我让jawr插件Javascript i18n消息生成器开始工作。使用bundle id
jawr.js.bundle.lib.id=/messages.js jawr.js.bundle.lib.mappings=messages:org.myapp.java.messages(mynamespace)[js.]
使用mynamespace(如jawr.properties中所定义)读取java资源包定义jawr.properties的方法非常简洁我可以访问JSP
window.mynamespace.property1