Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jsf messages: adding link

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\">" + 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">1234</a>.

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

Thanks in advance

~Zack

like image 566
Zack Marrapese Avatar asked Jun 23 '26 05:06

Zack Marrapese


2 Answers

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>
like image 95
Damo Avatar answered Jun 24 '26 20:06

Damo


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.

like image 33
Peter Hilton Avatar answered Jun 24 '26 19:06

Peter Hilton



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!