title: Add message from method in JSF
body: <p>I have request scope backing bean for login page( I am using Spring Security ). When authentication error occurs Spring put it to context and I am trying add error message to my page.</p>
  
  <pre><code>public void doLogin() throws IOException {
      final FacesContext context = FacesContext.getCurrentInstance();
      ExternalContext externalContext = context.getExternalContext();
      externalContext.dispatch("/j_spring_security_check");
  
      if (externalContext.getRemoteUser() == null) {
          Exception loginError = (Exception) externalContext.getSessionMap().get(
                  AbstractProcessingFilter.SPRING_SECURITY_LAST_EXCEPTION_KEY);
          if (loginError instanceof BadCredentialsException) {
              externalContext.getSessionMap().put(
                      AbstractProcessingFilter.SPRING_SECURITY_LAST_EXCEPTION_KEY, null);
              context.addMessage("loginBtn", new FacesMessage(FacesMessage.SEVERITY_ERROR, "Invalid credentials!", null));
              context.responseComplete();
          }
      }
  
      context.renderResponse();
  }
  </code></pre>
  
  <p>my page:</p>
  
  <pre><code>  &lt;a4j:form prependId="false"&gt;
          &lt;h:panelGrid columns="2" cellpadding="5" &gt;
              &lt;h:outputText value="#{msgs.login_username}"/&gt;
              &lt;h:inputText id="j_username"/&gt;
  
              &lt;h:outputText value="#{msgs.login_password}"/&gt;
              &lt;h:inputSecret id="j_password"/&gt;
  
              &lt;h:outputText value=" "/&gt;
              &lt;f:facet name="footer"&gt;
                  &lt;h:panelGroup&gt;
                      &lt;h:commandButton type="submit" id="loginBtn" action="#{guest.doLogin}" value="#{msgs.login}" /&gt;
                      &lt;rich:spacer width="5" height="1"/&gt;
                      &lt;rich:message id="errorForLogin" for="loginBtn"&gt;
                          &lt;f:facet name="errorMarker"&gt;
                              &lt;h:graphicImage value="./resources/forbidden.gif"/&gt;
                          &lt;/f:facet&gt;
                      &lt;/rich:message&gt;
                  &lt;/h:panelGroup&gt;
              &lt;/f:facet&gt;
          &lt;/h:panelGrid&gt;
          &lt;br/&gt;&lt;br/&gt;
      &lt;/a4j:form&gt;
  </code></pre>
  
  <p>But it's don't works. I am trying put error displaying code to @PostConstruct method but this didn't help. This work only if I implement my PhaseListener but I think this is bad idea because it invokes on every request. What my mistake, there are some ways add error message from method, maybe in @PostConstruct method?</p>