简体   繁体   中英

Single Curly Bracket in Properties file

I am trying to read this text which has only a single curly brace

Y8R30j)i{sjmPXfE

from a .properties file using MessageResources.getMessage()

and am getting this exception

java.lang.IllegalArgumentException: Unmatched braces in the pattern.
at java.text.MessageFormat.applyPattern(MessageFormat.java:508)
at java.text.MessageFormat.<init>(MessageFormat.java:363)

I tried to escape by using

Y8R30j)i'{'sjmPXfE

but am getting the same exception.

org.apache.struts.util.MessageResources uses java.text.MessageFormat which interprets things between curly braces as patterns or placeholders to be replaced with strings.

Per the exception it is clear that java is not able to find the closing brace for the opening curly brace you have in your key value, possible workaround (working with struts 1.3) is below. ( in light of unicode escaping or any other escaping not working, can refer to java.text.MessageFormat.applyPattern() method for further escaping possibles)

Specify key as below in message resources file -

key=Y8R30j)i{0}sjmPXfE

Read value of the key with the code below in your action (or any other java) class

MessageResources messages = MessageResources.getMessageResources("MessageResources");
Object[] leftCurlyBrace = { "{" };
String value = messages.getMessage(request.getLocale(), "key", leftCurlyBrace);

Am assuming you are trying to read some encrypted value from a properties file in a struts 1.x J2EE environment

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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