3

In Java using UTF-8 characters in a String is as easy as specifiying "\u2022". Afaik strings specified in the JSF expression language are Java strings, e.g. #{'some string'} (where '' is used to delimit the string because "" is already used to delimit XHTML attribute values).

How can I specify the above string "\u2022" in JSF XHTML, e.g. in the value of a h:outputLabel?

<h:outputLabel value="#{'\u2022'}"/>

fails due to /index.xhtml @9,45 value="#{'\u2022'}" Error Parsing: #{'\u2022'}.

<h:outputLabel value="\u2022"/>

results in u2022 being generated in XHTML.

The search for information is difficult because encoding on web pages is very important and the search results are dominated by explanations of it.

Referencing a String property in a backing bean is a valid workaround as well as using a resource bundle.

Kalle Richter
  • 8,008
  • 26
  • 77
  • 177
  • http://balusc.omnifaces.org/2009/05/unicode-how-to-get-characters-right.html – Kukeltje Aug 01 '18 at 18:11
  • @Kukeltje Thanks for your input. I tried that unsuccessfully (edited). The link to balusc's blog is valuable, but mostly covers encoding issues which are not the main problem here (more of a syntax issue), do you have a specific passage in the blog post in mind? – Kalle Richter Aug 03 '18 at 09:21
  • Maybe I miss what you effectively want to achieve (looking at the other answer). Your remarks about the ' are somewhat confusing – Kukeltje Aug 03 '18 at 09:40
  • @Kukeltje In Java you can specify a UTF-8 character as `String` or `char` (delimited with `""` and `''` respectively). In JSF I can't declare a `char` because `''` is used to delimit string because `""` is already used to delimit XHTML attributes and thus using `""` inside an attribute with is already delimited with `""` would cause unnecessary compiler challanges and make the code harder to read than necessary. I just wanted to avoid comments and answers explaining that. I'll remove it. – Kalle Richter Aug 03 '18 at 09:46
  • @Kukeltje I want to know the syntax to declare a UTF-8 character, e.g. 2022, in XHTML or an explanation why this isn't possible. – Kalle Richter Aug 03 '18 at 09:50
  • In the BalusC blog he talks about how the file is **locally** saved, e.g. in your IDE. Check those things. I don't see any other reason that the thing in my first comment should not work (or give you what you experience and not another weird character) – Kukeltje Aug 03 '18 at 10:55

2 Answers2

3

Usual JSF value attribute refers to a backing bean object which often delivers a String. This string supports HTML UTF-8 escaping like this &#xAAAA; where AAAA is the hexadecimal code of the unicode character.

So, simply write this :

<h:outputLabel value="&#x2022;" />

You can write this escape sequence in an Expression Language litteral as well :

<h:outputLabel value="#{'&#x2022;'}" />
fxrobin
  • 590
  • 3
  • 9
-1

XHTML is a XML markup language, and as such accepts escaping to xml entities:

<h:outputText value="&quot;User&quot;" />

will print

"User"

Reference: https://en.wikipedia.org/wiki/List_of_XML_and_HTML_character_entity_references#Predefined_entities_in_XML

You could also use a resource bundle, which will provide you with multilingal support and unicode encoding. Then you could use:

<h:outputText  value="#{msg.user}"/>

And in the messages.properties file:

user=\u2022User\u2022

See BalusC answer: Internationalization in JSF, when to use message-bundle and resource-bundle?

  • Thanks for you input. However I don't see how XML escaping is related to my question, please explain. Specifying the character in a resource bundle seems more complex than specifying it as a literal in a backing bean. It's another workaround. – Kalle Richter Aug 03 '18 at 09:23