HJW
HJW

Reputation: 23453

Overriding JSF Primefaces Message Tag

Can I override the default implementation of <p:message ../> to always implicitly have display="text" instead of typing out in full in time?

<p:message for="hotelName" display="text" />

Upvotes: 0

Views: 2387

Answers (2)

BalusC
BalusC

Reputation: 1109830

You could extend the PrimeFaces' MessageRenderer for that. Just override the encodeEnd() method wherein you set the default attribute before calling the super method.

Here's a self-containing kickoff example:

package com.example;

import java.io.IOException;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;

import org.primefaces.component.message.MessageRenderer;

public class CustomMessageRenderer extends MessageRenderer {

    @Override
    public void encodeEnd(FacesContext facesContext, UIComponent component) throws IOException {
        component.getAttributes().put("display", "text"); // You might want to check first if it isn't already set.
        super.encodeEnd(facesContext, component);
    }

}

which you need to register in faces-config.xml as follows (no, annotation magic won't work in overriding the existing renderers which are by itself registered by XML, so the XML is absolutely necessary):

<render-kit>
    <renderer>
        <component-family>org.primefaces.component</component-family>
        <renderer-type>org.primefaces.component.MessageRenderer</renderer-type>
        <renderer-class>com.example.CustomMessageRenderer</renderer-class>
    </renderer>
</render-kit>

But easier would be to just throw in a little bit of CSS to hide the icon.

.ui-message-error-icon {
    display: none;
}

Creating a composite component as suggested by the other answer isn't exactly trivial because the for target of the <p:message> isn't contained within the same composite.

Upvotes: 4

user793953
user793953

Reputation: 91

I would simply create a Facelet Composition Component for that.

Upvotes: 0

Related Questions