Reputation: 12090
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
Upvotes: 3
Views: 3810
Reputation: 1
If you're using jquery you can unescape the xml characters:
<script type="text/javascript">
//<![CDATA[
$(document).ready(function() {
$(".esc").each(function(i) {
var h = $(this).html();
h = h.replace(/</gi, "<");
h = h.replace(/>/gi, ">");
$(this).html(h);
});
});
//]]>
</script>
Upvotes: 0
Reputation: 11550
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>
Upvotes: 3
Reputation: 17344
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.
Upvotes: 2