3 Replies Latest reply on Jul 16, 2013 2:29 PM by Felipe Pina

    how to make a rich:message(s) with hyperlink

    u j Novice

      In rich:message and rich:messages all characters are escaped, so a hyperlink shows as plain text, as in h:message(s).

       

      For standard JSF there are solutions around like this:

      http://stackoverflow.com/questions/3356546/embedding-a-link-or-other-html-in-a-jsf-message

       

      I digged into the RichFaces code, but I am not sure how to achieve it for rich:message.

      Do I have to overwrite org.richfaces.renderkit.MessageRendererBase?

       

      It would be nice to have the escape attribute in rich:message by default.

      There is an old feature request for RichFaces 3:

      http://community.jboss.org/thread/145789?tstart=0

       

      But since I cannot wait for this, I need a solution now.

       

      Thanks in advance

        • 1. Re: how to make a rich:message(s) with hyperlink
          Brian Leathem Master

          The relevant RichFaces jira issue is: https://issues.jboss.org/browse/RF-8226

           

          I remeber asking that question on stack overflow...

           

          I ended up implementing a custom renderer for the vanilla jsf solution:

          @FacesRenderer(componentFamily="javax.faces.Messages", rendererType="javax.faces.Messages")
          public class MessagesRenderer extends com.sun.faces.renderkit.html_basic.MessagesRenderer {
          
          
              @Override
              public void encodeEnd(FacesContext context, UIComponent component) throws IOException {
                  ResponseWriter oldWriter = context.getResponseWriter();
                  ResponseWriter newWriter = new NoEscapeWriter(oldWriter, oldWriter.getContentType(), oldWriter.getCharacterEncoding());
                  context.setResponseWriter(newWriter);
                  super.encodeEnd(context, component);
                  context.setResponseWriter(oldWriter);
              }
          
          
              @Override
              public void encodeBegin(FacesContext context, UIComponent component) throws IOException {
                  ResponseWriter oldWriter = context.getResponseWriter();
                  ResponseWriter newWriter = new NoEscapeWriter(oldWriter, oldWriter.getContentType(), oldWriter.getCharacterEncoding());
                  context.setResponseWriter(newWriter);
                  super.encodeBegin(context, component);
                  context.setResponseWriter(oldWriter);
              }
          }
          

           

          and registered it via the faces-config:

              <render-kit>
                  <renderer>
                      <component-family>javax.faces.Messages</component-family>
                      <renderer-type>javax.faces.Messages</renderer-type>
                      <renderer-class>
                  ca.triumf.mis.qms.workrequest.jsf.renderer.MessagesRenderer
                      </renderer-class>
                  </renderer>
              </render-kit>
          

           

          The same approach should work in this case.  Just override the RichFaces classes, instead of the mojarra ones.

          This is a non-invasive (in that you don't have to change richfaces code) solution, which is good.  You can drop this code from your project as soon as it becomes available in RichFaces.

          • 2. Re: how to make a rich:message(s) with hyperlink
            u j Novice

            I tried

             

            import java.io.IOException;

             

            import javax.faces.component.UIComponent;

            import javax.faces.context.FacesContext;

            import javax.faces.context.ResponseWriter;

            import javax.faces.context.ResponseWriterWrapper;

            import javax.faces.render.FacesRenderer;

             

            import org.richfaces.renderkit.html.HtmlMessagesRenderer;

             

            @FacesRenderer(componentFamily="javax.faces.Messages", rendererType="javax.faces.Messages")

            public class NoEscapeMessagesRenderer extends HtmlMessagesRenderer {

              @Override

              public void encodeEnd(FacesContext context, UIComponent component) throws IOException {

                  ResponseWriter oldWriter = context.getResponseWriter();

                  ResponseWriter newWriter = new NoEscapeWriter(oldWriter);

                  context.setResponseWriter(newWriter);

                  super.encodeEnd(context, component);

                  context.setResponseWriter(oldWriter);

              }

             

             

              @Override

              public void encodeBegin(FacesContext context, UIComponent component) throws IOException {

                  ResponseWriter oldWriter = context.getResponseWriter();

                  ResponseWriter newWriter = new NoEscapeWriter(oldWriter);

                  context.setResponseWriter(newWriter);

                  super.encodeBegin(context, component);

                  context.setResponseWriter(oldWriter);

              }

             

                class NoEscapeWriter extends ResponseWriterWrapper {

                    ResponseWriter originalWriter;

             

                    public NoEscapeWriter(ResponseWriter originalWriter) {

                        this.originalWriter = originalWriter;

                    }

             

                    @Override

                    public ResponseWriter getWrapped() {

                        return originalWriter;

                    }

             

                    @Override

                    public void writeText(Object text, UIComponent component, String property)

                            throws IOException {

                        String string = String.valueOf(text);

                        String escape = (String) component.getAttributes().get("escape");

                        if (escape != null && !Boolean.valueOf(escape)) {

                            super.write(string);

                        }

                        else {

                            super.writeText(string, component, property);

                        }

                    }

                }

            }

             

            and registered it in faces-config.xml, but it is never used when I produce rich:messages or rich:message.

            Am I extending the wrong class?

             

            Thanks

            • 3. Re: how to make a rich:message(s) with hyperlink
              Felipe Pina Newbie

              In richfaces use this code below:

               


              <render-kit>

                  <renderer>

                      <component-family>org.richfaces.component.RichMessages</component-family>

                      <renderer-type>org.richfaces.RichMessagesRenderer</renderer-type>

                      <renderer-class>com.sample.EscapableMessagesRenderer</renderer-class>

                  </renderer>

              </render-kit>

               

              public class EscapableMessagesRenderer extends org.richfaces.renderkit.html.HtmlRichMessagesRenderer

               

              Regards..