Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
466 views
in Technique[技术] by (71.8m points)

xhtml - How to insert special characters like & and < into JSF components' value attribute?

How to insert special characters like & and < into JSF components value attribute ?

For example: I want something like this:

<h:outputText value="Tom & Jerry Show" />

When I try this, I get the following exception:

javax.faces.view.facelets.FaceletException: Error Parsing /foo.xhtml: Error Traced[line: 15] The entity name must immediately follow the '&' in the entity reference.

And in case of <, I get the following exception:

javax.faces.view.facelets.FaceletException: Error Parsing /foo.xhtml: Error Traced[line: 15] The value of attribute "value" associated with an element type "h:outputText" must not contain the '<' character.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You need to escape them to XML entities.

<h:outputText value="Tom &amp; Jerry Show" />

This problem is not related to JSF per se, but to the view technology. You're apparently using Facelets (which is perfectly fine!). Facelets is however a XML based view technology, so you would need to ensure that the template is well formed XML and that all special characters which are to be represented as-is, are been escaped like above.

In the wikipedia you can find a list of predefinied character entities which needs to be escaped in the XML whenever you'd like to display them as-is. It are the following character entities:

"     &quot;
&     &amp;
'     &apos;
<     &lt;
>     &gt;

See also:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...