简体   繁体   English

在Javascript中使用Resourcebundle属性

[英]Using Resourcebundle Properties in Javascript

I work on a Java EE web application that uses a combination of Dojo and plain javascript for the front-end. 我在Java EE Web应用程序上工作,该应用程序使用Dojo和普通javascript的组合作为前端。

We've discovered that when ResourceBundle properties are used in javascript, in some cases they end up breaking code. 我们发现,在JavaScript中使用ResourceBundle属性时,在某些情况下,它们最终会破坏代码。 Specifically, this happens when the properties contain quotes (single and double) & escape sequences (\\n, \\s ...). 具体来说,当属性包含引号(单引号和双引号)和转义序列(\\ n,\\ s ...)时,会发生这种情况。

The solution seems to be to include extra escape characters. 解决方案似乎是包括额外的转义字符。 For instance, \\n needs to be prepended by one more slash (\\\\n) when used in a Js alert to correctly render the line break, and Quotes if not escaped truncate the content prematurely for obvious reasons. 例如,在Js警报中使用\\ n时,必须在其前面加上一个斜杠(\\\\ n),以正确呈现换行符;如果没有转义,则Quotes会由于明显的原因而过早地截断内容。

Our solution to the above issues so far has been to put in the extra escape characters in the property files itself. 到目前为止,我们对上述问题的解决方案是在属性文件本身中放入多余的转义字符。 But this is something that we would like to move away from. 但这是我们想要摆脱的。 It seems like this might be a widespread problem and I'd like to hear from the experts on how you might have solved this problem. 看来这可能是一个普遍存在的问题,我想听听专家们关于您可能如何解决此问题的信息。

Current Usage: key=A newline is represented with \\\\n and this \\" is within quotes \\". 当前用法: key=A newline is represented with \\\\n and this \\" is within quotes \\".

Envisioned Usage : key=A newline is represented with \\n and this " is within quotes ". 预期的用法: key=A newline is represented with \\n and this " is within quotes ".

PS: We typically use the <fmt:message> tag to access these values in the front end and for use in javascript. PS:我们通常使用<fmt:message>标记在前端访问这些值并在javascript中使用。

Consider using StringUtils. 考虑使用StringUtils。 If has a method to escape input like yours. 如果有一种方法可以像您一样转义输入。

http://commons.apache.org/lang/api-2.5/org/apache/commons/lang/StringEscapeUtils.html#escapeJava(java.lang.String) http://commons.apache.org/lang/api-2.5/org/apache/commons/lang/StringEscapeUtils.html#escapeJava(java.lang.String)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用ResourceBundle仅读取一些属性 - read only some properties using ResourceBundle 资源束vs属性 - resourcebundle vs properties 使用ResourceBundle的Java本地化文本Web服务(locale.properties) - Java Localized text Web services using ResourceBundle(locale.properties) 使用ResourceBundle将属性文件从一种语言翻译成另一种语言 - Translate the Properties File from one language to another using ResourceBundle 从JFileChooser获取file.properties并在ResourceBundle上使用它 - getting file.properties from JFileChooser and using it on ResourceBundle 为什么不使用ResourceBundle而不是Properties? - Why not use ResourceBundle instead of Properties? 从 ResourceBundle 生成属性 object? - Generate a Properties object from a ResourceBundle? 如何在属性文件中指定值,以便可以使用ResourceBundle#getStringArray检索它们? - How do I specify values in a properties file so they can be retrieved using ResourceBundle#getStringArray? 如何在使用ResourceBundle API获取的属性文件中转义花括号 - How to escape curly braces in my properties file which is fetched using the ResourceBundle API 使用类而不是属性文件或 ListResourceBundles 时 ResourceBundle.getBundle() 的强制语言 - Mandatory language for ResourceBundle.getBundle() when using classes instead of properties files or ListResourceBundles
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM