5 Replies Latest reply on Jul 27, 2007 5:20 PM by sergeysmirnov

    Unable to render Error Message <h:message> on JSF page

    mail2bansi

      I have a textfield on jsf page , the onblur event of textfield calls a backing bean method . This method in certain situation catches Exception and calls FacesContext to render error message on jsf page. The problem is message doesnt get render on to jsf page inspite of having h:message tag.

      Here is the snippet

      JSF page
      <h:panelGrid columns="3" styleClass="detail" columnClasses="label" >
      <h:outputLabel><h:outputText value="Property Tag" /> </h:outputLabel>
      <h:inputText id="propertyTag" value="#{deviceBean.tagNumber}" >
      <a4j:support action="#{deviceBean.loadAssetDetails}" event="onblur" reRender="mypanel" />
      </h:inputText>
      <h:message id="propertyTagError" for="propertyTag" styleClass="errorMessage" />
      </h:panelGrid>

      Backing Bean
      public String loadAssetDetails() {
      try{
      namsAsset = deviceManager.getAssetDetails(sourceSystem, tagNumber); //calls database
      }catch(RuntimeException re){

      if (re.getMessage().contains("No Asset found")){
      System.out.println("Inside Bean No Asset Exception");
      FacesContext.getCurrentInstance().addMessage("deviceForm:propertyTag", new FacesMessage(FacesMessage.SEVERITY_ERROR, re.getMessage(), re.getMessage()));
      return null;
      }
      }
      The log file prints the messages inside the catch block. But it doesnt get render on JSF page. Any pointers/suggestions will be highly appreciated

        • 1. Re: Unable to render Error Message <h:message> on JSF page

          I do not see you did something to have the error message visible during the Ajax requests.

          At least, surround your h:message with a4j:outputPanel ajaxRendered="true"

          • 2. Re: Unable to render Error Message <h:message> on JSF page
            mail2bansi

            Hi Sergey
            The following snippet should make the error message visible
            <ui:include src="messages.xhtml"/>
            <h:messages errorClass="error" layout="table" style="width: 100%"/>

            It works in almost all cases except in this particular case.

            Please note <h:message> belongs to textfield , the onblur event of which loads remaining fields on form which are under <a4j:outputPanel>

            I have added ajaxRendered="true" to it but it didnt made any difference


            <h:inputText id="propertyTag" value="#{deviceBean.tagNumber}" >
             <a4j:support action="#{deviceBean.loadAssetDetails}" event="onblur" reRender="mypanel" />
             </h:inputText>
             <h:message for="propertyTag" styleClass="errorMessage" />
            
            


            <a4j:outputPanel id="mypanel" ajaxRendered="true" >
            
             <h:panelGrid columns="3" styleClass="detail" columnClasses="label" >
             <h:outputLabel><h:outputText value="Model" /> </h:outputLabel>
             <h:inputText id="model" value="#{deviceBean.device.model}" disabled="#{deviceBean.disableMode}"/>
             <h:message for="model" styleClass="errorMessage" />
             </h:panelGrid>
            </a4j:outputPanel>
            


            public String loadAssetDetails() {
            try{
             namsAsset = deviceManager.getAssetDetails(sourceSystem, tagNumber);
             }catch(RuntimeException re){
            
             if (re.getMessage().contains("No Asset found")){
             System.out.println("Inside Bean No Asset Exception");
            
             FacesUtils.addErrorMessage("errors.NoAsset", sourceSystem, tagNumber);
            
             return null;
             }
            
            


            public static void addErrorMessage(String key, String arg1, String arg2) {
             //FacesContext.getCurrentInstance().addMessage(clientId, new FacesMessage(FacesMessage.SEVERITY_ERROR, msg, msg));
             ApplicationFactory factory =
             (ApplicationFactory) FactoryFinder.getFactory(FactoryFinder.APPLICATION_FACTORY);
             String bundleName = factory.getApplication().getMessageBundle();
            
             ResourceBundle messages = ResourceBundle.getBundle(bundleName);
            
            
             MessageFormat form = new MessageFormat(messages.getString(key));
            
             String msg = form.format(new Object[] { arg1, arg2 });
            
            
            
             System.out.println("Inside Error Message="+msg);
             FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, msg, msg));
            
            
             }
            
            


            • 3. Re: Unable to render Error Message <h:message> on JSF page

              In the java code, you create a global message (the first param is null). However, on the page, you have only the couple h:message and no h:messages that show the global messages.
              Could you explain the point?

              • 4. Re: Unable to render Error Message <h:message> on JSF page
                mail2bansi

                Sergey
                <h:messages> is there on the top of the xhtml page to print global messages. Here is the snippet

                <ui:include src="messages.xhtml"/>
                <h:messages errorClass="error" layout="table" style="width: 100%"/>
                


                • 5. Re: Unable to render Error Message <h:message> on JSF page

                  However, it is not updated during the Ajax declaring like this. So what is the point to set the message, but do not show it on the page?