Jsf posts: add link

Currently, in JSF, all HTML contained in a message (tag rich:messages) is escaped and displayed only as markup. For example, in my bean support, 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 face context and is then viewed in the tag rich:messages.

When this message is created, my message simply appears with escaped HTML:

Title created successfully with product number: <a href="http://www.example.com">1234</a>.

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

Thank you in advance

~ Zack

+3
source share
3 answers

The quick fix is ​​to create a new renderer.

h: messages, div. , .

, /:

public class MessagesRenderer extends HtmlBasicRenderer

ResponseWriter, . - HtmlResponseWriter, .

public void writeText(Object text, String componentPropertyName)

HtmlUtils.

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>
+3

, rich:messages, escape, h:outputText, HTML-.

+2

jquery, xml:

<script type="text/javascript">
         //<![CDATA[
        $(document).ready(function() {
            $(".esc").each(function(i) {
                var h = $(this).html();                
                h = h.replace(/&lt;/gi, "<");
                h = h.replace(/&gt;/gi, ">");
                $(this).html(h);
            });
        });
        //]]> 
     </script>
0

Source: https://habr.com/ru/post/1707686/


All Articles