[英]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.