How to escape curly brackets in .properties file in Struts 1.2
Asked Answered
N

4

6

I have a requirement to retain the curly brackets from properties file in my rendered JSP code. 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

The problem is that struts processes the {} as a placeholder for a runtime value. Any help greatly appreciated.

Nakisha answered 7/1, 2011 at 11:40 Comment(2)
have you tried parsing both the braces like text '{'some text'}' more text ?Nakisha
Thanks for your reply. Yes, it didn't help.Nakisha
N
9

see here 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.

Nakisha answered 7/1, 2011 at 11:51 Comment(0)
A
1

I jumped to this problem just 10 minutes ago. This is my try: put '&#123' instead of '{'. (Reference: http://www.asciitable.com/)

Don't use Oct or Hex code since Java will parse these numbers into { before actually reading the key; therefore, it will lead to the same problem. Only HTML code will work, since Java will not parse it, but the JSP page will

Aikens answered 17/10, 2012 at 6:52 Comment(0)
A
0

From the MessageFormat spec: 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; the surrounding single quotes are removed. An UnquotedString can contain arbitrary characters except single quotes and left curly brackets. Thus, a string that should result in the formatted message "'{0}'" can be written as "'''{'0}''" or "'''{0}'''".

which means you should probably try to quite your curly braces like this '''{'.

Alethiaaletta answered 7/1, 2011 at 12:56 Comment(0)
U
0

Since single quote ' is always doubled by struts before it arrives into MessageFormat, you can not produce '''{' as suggested by @Mihai Toader. Or any other odd-numbered count of them required by MessageFormat

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
Unionist answered 10/7, 2020 at 19:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.