PrimeFaces Beginner's Guide
上QQ阅读APP看书,第一时间看更新

Time for action – displaying FacesMessage using <p:message>

Let us see how to display FacesMessages with different display modes and severity levels.

  1. Create a form with three required input fields and display validation errors using <p:message> as follows:
    <h:form id="userRegForm" style="width: 500px; padding-left: 10px;">
      <p:panel header="Messages - Using 'display' modes" >
        <h:panelGrid columns="3">
          <p:outputLabel value="EmailId"/>
          <p:inputText id="emailId" value="#{messagesController.user.emailId}" required="true" requiredMessage="Enter EmailId"/>
          <p:message for="emailId"/>
          
          <h:outputText value="Password" />
          <p:password id="password" value="#{messagesController.user.password}" required="true" requiredMessage="Enter Password"/>
          <p:message  for="password" display="text"/>
          
          <h:outputText value="FirstName" />
          <p:inputText id="firstName" value="#{messagesController.user.firstName}" required="true" requiredMessage="Enter FirstName"/> 
          <p:message for="firstName" display="icon" severity="warn, error" />
          
          <p:commandButton value="Submit" actionListener="#{messagesController.doRegister}" update="userRegForm"/>
        </h:panelGrid>
      </p:panel>
    </h:form>

What just happened?

We have displayed error messages using the <p:message> component for each of the input fields. We have also used different types of display modes. We have restricted FirstName input field messages to display only warn and error severity messages.

What just happened?

Displaying messages with HTML content

The Message component replaces the HTML content by default. You can use the escape attribute to turn on/off this behavior. By default, escape is set to TRUE.