简体   繁体   English

如何在 Struts 1.2 中的 .properties 文件中转义大括号

[英]How to escape curly brackets in .properties file in Struts 1.2

I have a requirement to retain the curly brackets from properties file in my rendered JSP code.我需要在渲染的 JSP 代码中保留属性文件中的大括号。 Is it possible?有可能吗?

I have tried options like: \\{ , '{ , {{ , '{' , /{ , but no help.我尝试过以下选项: \\{ , '{ , {{ , '{' , /{ ,但没有帮助。

At runtime, I get an exception:在运行时,我得到一个异常:

java.lang.IllegalArgumentException: can't parse argument number java.lang.IllegalArgumentException:无法解析参数编号

The problem is that struts processes the {} as a placeholder for a runtime value.问题在于 struts 将{}作为运行时值的占位符处理。 Any help greatly appreciated.非常感谢任何帮助。

see here http://struts.apache.org/2.x/docs/how-to-escape-special-chars-in-resource-bundles.html见这里http://struts.apache.org/2.x/docs/how-to-escape-special-chars-in-resource-bundles.html

use '{' and '}' to escape.使用'{''}'进行转义。


Just re-read your question looks like it didn't help.重新阅读您的问题似乎没有帮助。

I jumped to this problem just 10 minutes ago.就在 10 分钟前,我跳到了这个问题。 This is my try: put '&#123' instead of '{'.这是我的尝试:把“&#123”而不是“{”。 (Reference: http://www.asciitable.com/ ) (参考: http : //www.asciitable.com/

Don't use Oct or Hex code since Java will parse these numbers into { before actually reading the key;不要使用 Oct 或 Hex 代码,因为 Java 会在实际读取密钥之前将这些数字解析为 {; therefore, it will lead to the same problem.因此,它会导致同样的问题。 Only HTML code will work, since Java will not parse it, but the JSP page will只有 HTML 代码可以工作,因为 Java 不会解析它,但 JSP 页面会

From the MessageFormat spec: http://download.oracle.com/javase/1.4.2/docs/api/java/text/MessageFormat.html从 MessageFormat 规范: http : //download.oracle.com/javase/1.4.2/docs/api/java/text/MessageFormat.html

Within a String, "''" represents a single quote.在字符串中,“''”代表单引号。 A QuotedString can contain arbitrary characters except single quotes; QuotedString 可以包含除单引号之外的任意字符; the surrounding single quotes are removed.周围的单引号被删除。 An UnquotedString can contain arbitrary characters except single quotes and left curly brackets. UnquotedString 可以包含除单引号和左大括号之外的任意字符。 Thus, a string that should result in the formatted message "'{0}'" can be written as "'''{'0}''" or "'''{0}'''".因此,应生成格式化消息“'{0}'”的字符串可以写为“'''{'0}''”或“'''{0}'''”。

which means you should probably try to quite your curly braces like this '''{' .这意味着您可能应该尝试像这样'''{'完全使用大括号。

Since single quote ' is always doubled by struts before it arrives into MessageFormat , you can not produce '''{' as suggested by @Mihai Toader .由于单引号'在到达MessageFormat之前总是被struts加倍,因此您不能按照@Mihai Toader 的建议生成'''{' Or any other odd-numbered count of them required by MessageFormatMessageFormat所需的任何其他奇数计数

I have ended up using parameters with value { and }我最终使用了值为{}参数

my.property = You have ${0}count{1} unread messages

If you supply parameter values { and } , you get this如果你提供参数值{} ,你会得到这个

You have ${count} unread messages

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM