title: Error messages on page with the <h:messages> in JSF
body: <p>I'm creating simple example on JSF. It's small Login application. My problem is duplicate error messages on my page:</p>
  
  <p><img src="https://i.stack.imgur.com/08aHs.jpg" alt="my screen"></p>
  
  <p>I have two <code>h:message</code>(for username and password) tags and one <code>h:messages</code>(global messages) on my page:</p>
  
  <pre><code>&lt;h:form id="loginForm"&gt;
  
     &lt;h:panelGrid columns="3"&gt;
  
         &lt;h:outputText value="Username" /&gt;
         &lt;h:inputText id="username" value="#{loginBean.username}" 
                    required="true" requiredMessage="Please enter your username" /&gt;
         &lt;h:message for="username" errorClass="errorMessage" /&gt; &lt;!-- Message for username --&gt;
  
         &lt;h:outputText value="Password" /&gt;
         &lt;h:inputSecret id="password" value="#{loginBean.password}" 
                    required="true" requiredMessage="Please enter your password" /&gt;
         &lt;h:message for="password" errorClass="errorMessage" /&gt; &lt;!-- Message for password --&gt;
  
  
         &lt;h:commandButton value="Login" action="#{loginBean.login}" /&gt;
  
     &lt;/h:panelGrid&gt;
  
     &lt;h:messages styleClass="errorMessage" /&gt; &lt;!-- Global messages --&gt;
  
  
  &lt;/h:form&gt;
  </code></pre>
  
  <p>Action method <code>login()</code> in my backing bean:</p>
  
  <pre><code>public String login() {
      FacesContext facesContext = FacesContext.getCurrentInstance();
      if ("admin".equals(username) &amp;&amp; "pass".equals(password)) {
          return "success";
      } else {
          facesContext.addMessage("loginForm", new FacesMessage("Username or password is incorrect"));
          return null;
      }
  }
  </code></pre>
  
  <p>I want to display <strong>message about empty username or password only next to these fields</strong>, not under my button in global messages. And I want to print my error <strong>message from my action method in global messages</strong> if password or username is incorrect. How can I resolve this?</p>
  
  <p>I tried to use the attribute <code>globalOnly</code>:</p>
  
  <pre><code>&lt;h:messages styleClass="errorMessage" globalOnly="true" /&gt; &lt;!-- Global messages --&gt;
  </code></pre>
  
  <p>but it isn't completely solved my problem, because global messages aren't displayed at all in this case.</p>
  
  <p>Thanks in advance!</p>