JSF error message - Java JSF

Java examples for JSF:Message

Description

JSF error message

Demo Code

/**//  w  w w .ja  v  a2 s.c  o m
 * License: src/main/resources/license/escidoc.license
 */
import javax.faces.application.FacesMessage;
import javax.faces.application.FacesMessage.Severity;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import org.apache.log4j.Logger;

public class Main{
    public static void main(String[] argv) throws Exception{
        String summary = "java2s.com";
        error(summary);
    }
    /**
     * @param summary summary text
     */
    public static void error(String summary) {
        error(summary, null, null);
    }
    /**
     * @param summary summary text
     */
    public static void error(String summary, String detail) {
        error(summary, detail, null);
    }
    /**
     * @param component associated <code>UIComponent</code>
     * @param summary summary text
     */
    public static void error(UIComponent component, String summary) {
        error(summary, null, component);
    }
    /**
     * @param summary summary text
     */
    public static void error(String summary, String detail,
            UIComponent component) {
        message(summary, detail, component, FacesMessage.SEVERITY_ERROR);
    }
    /**
     * @param summary summary text
     */
    public static void message(String summary, String detail,
            UIComponent component, Severity severity) {
        FacesMessage fm = new FacesMessage(severity, summary, detail);
        if (component == null) {
            FacesContext.getCurrentInstance().addMessage(null, fm);
        } else {
            FacesContext.getCurrentInstance().addMessage(component.getId(),
                    fm);
        }
    }
}

Related Tutorials