I agree with user423943 in the idea of creating a component for that. However, I would extend the <h:outputText>
instead. In your case, you will not have a lot of work to do. First, create a my.taglib.xml
file:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE facelet-taglib PUBLIC "-//Sun Microsystems, Inc.//DTD Facelet Taglib 1.0//EN" "facelet-taglib_1_0.dtd">
<facelet-taglib>
<namespace>http://my.components/jsf</namespace>
<tag>
<tag-name>myComponent</tag-name>
<component>
<component-type>my.component.myComponent</component-type>
<renderer-type>my.renderkit.myComponent</renderer-type>
</component>
</tag>
</facelet-taglib>
This file just need to be present in the classpath of your application and it will be loaded automatically by Facelets (because it ends with .taglib.xml
).
Then, in the faces-config.xml
defines the Java classes for this component:
<component>
<component-type>my.component.myComponent</component-type>
<component-class>my.package.component.MyHtmlComponent</component-class>
</component>
<render-kit>
<render-kit-id>HTML_BASIC</render-kit-id>
<renderer>
<component-family>javax.faces.Output</component-family>
<renderer-type>my.renderkit.myComponent</renderer-type>
<renderer-class>my.package.component.MyHtmlComponentRenderer</renderer-class>
</renderer>
Then, you will have to create two classes:
my.package.component.MyHtmlComponent
that will extend javax.faces.component.html.HtmlInputText
and do nothing more.
my.package.component.MyHtmlComponentRenderer
that will extend the com.sun.faces.renderkit.html_basic.TextRenderer
class.
Your renderer class will do all the job, by generating the HTML code for the value of your component, exactly as the <h:outputText>
does. You can have a look at HtmlBasicRenderer.encodeEnd(FacesContext, UIComponent)
and TextRenderer.getEndTextToRender(FacesContext, UIComponent, String)
methods, that are involved in this part.
Of course, when you are facing a {niceImage}
code in your text, you simply need to generate a HTML img
tag. For that, you can use the adequate methods of the ResponseWriter
to build an HTML tag and attributes:
writer.startElement("img", component);
writer.writeAttribute("src", urlToImage);
writer.endElement("img");
Once everything is created, you have to use your new component in your JSF page:
<html xmlns:my="http://my.components/jsf">
...
<my:myComponent value="#{myBean.dynamicHTMLContent}" escape="false"/>
...
Two links that can help you in addition to the ones provided by user423943:
http://www.jsftutorials.net/helpDesk/standardRenderKit_component-class_renderer-slass.html
http://www.jsftutorials.net/helpDesk/standardRenderKit_component-type_renderer-type.html
You will find, for all HTML JSF components their types and classes.