Search code examples
xmljsffaceletsjsf-2.2

How to escape special characters in taglib files in JSF?


I have created a custom tag in JSF as follows as defined in my.taglib.xml file.

<namespace>http://example.com/ui</namespace>

<tag>
    <tag-name>viewParamValidationFailed</tag-name>
    <attribute>
        <name>redirect</name>
        <required>true</required>            
        <type>java.lang.String</type>
        <description>Location to redirect, in case validation/s or conversion/s of view parameters as specified by the <![CDATA[<o:viewParam>]]> tag fails.</description>
    </attribute>

    <handler-class>tags.ViewParamValidationFailed</handler-class>
</tag>

This tag can be used on XHTML pages as follows.

xmlns:my="http://example.com/ui"

<my:viewParamValidationFailed redirect="Location.jsf"/>

I'm using NetBeans IDE 8.0. When ctrl+space is pressed to show the description of the redirect attribute, the IDE shows the following text.

Location to redirect, in case validation/s or conversion/s of view parameters as specified by the tag fails.

Please notice that <o:viewParam> enclosed within CDATA in the tag description is missing in this text- as specified by the___tag fails.

How to escape such special characters in such taglib files so that the description is shown correctly?


Solution

  • It seems that the contents of the description box are similar to the escaped HTML that you can include in doclets (used in Javadocs). So, if you want to make part of your description bold, you can't enclose the text in a <strong> element, but you must enclose it in an escaped version of it: &lt;strong&gt;. For example, if you use:

    <description>Location to &lt;strong&gt;redirect&lt;/strong&gt;, in ...</description>
    

    The text will appear in the description box as

    Location to redirect, in ...

    but if you use regular tags they will simply disappear.

    That means that if you actually want to display the tag, you will need to double the escapes. Either use &amp;&lt; and &amp;gt; for the angle brackets, or use escaped angle brackets inside the CDATA. I didn't test it but I am quite certain that this should work:

    <![CDATA[&lt;o:viewParam&gt;]]>