add Error Message to JSF FacesContext - Java JSF

Java examples for JSF:FacesContext

Description

add Error Message to JSF FacesContext

Demo Code


import java.util.ResourceBundle;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;

public class Main{
    public static void main(String[] argv) throws Exception{
        String key = "java2s.com";
        addErrorMessage(key);//from   w w  w.j a va2s.c o  m
    }
    public static void addErrorMessage(String key) {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        facesContext.addMessage("", createErrorMessage(key));
    }
    public static FacesMessage createErrorMessage(String key) {
        return new FacesMessage(FacesMessage.SEVERITY_ERROR, "",
                getMessages(key));

    }
    public static String getMessages(String key) {
        FacesContext context = FacesContext.getCurrentInstance();
        ResourceBundle bundle = ResourceBundle.getBundle(
                "com.formation.bundle.messagesErr", context
                        .getApplication().getDefaultLocale());
        String message = null;
        try {
            message = bundle.getString(key);
        } catch (Exception e) {
            return "unkown message " + key;
        }
        return message;
    }
}

Related Tutorials