1

I can display an error/info message when a button is clicked using JSF, but I can't display a message when a page is loaded.

Please try the following.

index.xhtml:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://xmlns.jcp.org/jsf/html">
    <h:head>
        <title>Facelet Title</title>
    </h:head>
    <h:body>
        <h:form id="componentForm">
            <h:messages errorStyle="color: red" infoStyle="color: green" globalOnly="true" />
                Enter the word Java here:
            <h:inputText id="javaText" value="#{messageController.javaText}" />
            <h:message for="javaText" errorStyle="color: red" infoStyle="color: green" />
            <h:commandButton id="addMessage" action="#{messageController.newMessage}"
                         value="New Message" />
        </h:form>
    </h:body>
</html>

MessageController.java:

@Named(value = "messageController")
@SessionScoped
public class MessageController implements Serializable {

    int hitCounter = 0;
    private String javaText;

    public MessageController() {
        javaText = null;
        FacesMessage facesMsg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Managed Bean Initialized", null);
        FacesContext.getCurrentInstance().addMessage(null, facesMsg);
    }

    public void newMessage() {
        String hitMessage = null;
        hitCounter++;
        if (hitCounter > 1) {
            hitMessage = hitCounter + " times";
        } else {
            hitMessage = hitCounter + " time";
        }

        Date currDate = new Date();
        FacesMessage facesMsg = new FacesMessage(FacesMessage.SEVERITY_ERROR, "You've pressed that button " + hitMessage + "! The current date and time: " + currDate, null);
        FacesContext.getCurrentInstance().addMessage(null, facesMsg);

        if (getJavaText().equalsIgnoreCase("java")) {
            FacesMessage javaTextMsg = new 
            FacesMessage(FacesMessage.SEVERITY_INFO, "Good Job, that is the correct text!", null);
            FacesContext.getCurrentInstance()
            .addMessage("componentForm:javaText", javaTextMsg);
        } else {
            FacesMessage javaTextMsg = new 
            FacesMessage(FacesMessage.SEVERITY_ERROR, "Sorry, that is NOT the correct text!", null);
            FacesContext.getCurrentInstance()
            .addMessage("componentForm:javaText", javaTextMsg);
        }
    }

    public String getJavaText() {
        return javaText;
    }

    public void setJavaText(String javaText) {
        this.javaText = javaText;
    }

}

Every message is displayed correctly except for one message that is created by a constructor.

I want to display the message Managed Bean Initialized when the page is loaded.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
tet
  • 1,287
  • 1
  • 17
  • 36

1 Answers1

0

I Got the same issue, Unfortunately FacesContext only init when page loaded.

So you can not use FacesContext.getCurrentInstance().addMessage() when page is initializing :(

There are 2 solution that i tried

  • When document ready, call ajax to show message

  • Pass String message and display it

Minh
  • 83
  • 5
  • _"So you can not use FacesContext.getCurrentInstance().addMessage() when page is initializing :("_ Yes you can, check the duplicate. That 100% what is done there – Kukeltje Jun 18 '18 at 16:53