views:

755

answers:

3

Currently in JSF, all HTML contained within a message (rich:messages tag) is escaped and just shows up as the markup. For example, in my backing bean, I have:

createMessage("Title created successfully with product number: <a href=\"http://www.example.com\"&gt;" + product.getProductNumber() + "</a>.");

where createMessage() is just a helper function that adds a new Message to the faces context and is then viewable in my rich:messages tag.

When this message is created, my message simply shows up with the escaped HTML:

Title created successfully with product number: <a href="http://www.example.com"&gt;1234&lt;/a&gt;.

Is there any way to avoid this and just provide an actual link in the message instead?

Thanks in advance

~Zack

+2  A: 

It sounds like you need to create your own version of rich:messages that has an escape attribute, like h:outputText, so you can disable HTML escaping.

Peter Hilton
+1  A: 

A quick solution is to create a new renderer.

I've done this for h:messages as I wanted to separate the messages of different severities into separate divs. If you never want to use the default renderer then it's a good option.

The standard class that you would overwrite/extend is:

public class MessagesRenderer extends HtmlBasicRenderer

You would just use a ResponseWriter that doesn't escape the text. The concrete class is the HtmlResponseWriter which escapes the text. You could extend this and overwrite the

public void writeText(Object text, String componentPropertyName)

so that it doesn't use HtmlUtils.

Then just add your new renderer to faces-config.xml

<render-kit>
    <renderer>
     <component-family>javax.faces.Messages</component-family>
     <renderer-type>javax.faces.Messages</renderer-type>
     <renderer-class>com.mypackage.MessagesRenderer</renderer-class>
    </renderer>
</render-kit>
Damo
A: 
Skx