Search code examples
jsffacelets

Error message doesn't display


I have a xhtml page with code as below

            <table width="100%" cellpadding="2">
                <tr>
                    <td><h:form>
                            <h:commandButton
                                action="#{TypeController.addType}"
                                value="#{msgs.add}" />
                        </h:form></td>
                    <td width="25%"></td>
                </tr>
            </table>

            <h:form id="mainTable">
                <h:message for="main_content" errorClass="errorClass" />
                <p:dataTable id="dt1" width="100%"
                    value="#{dataTypeListBean.dataTypeBackingBeans}"
                    var="item" border="2" cellpadding="2"
                    styleClass="table_style">
                     <p:column>

....................

             <p style="float: right;">
                                    <h:commandLink
                                        action="#{DictionaryTypeController.delete}"
                                        onclick="return confirm('#{msgs.remove_confirm}');">
                                        <f:setPropertyActionListener
                                            target="#{DictionaryTypeController.selectedType}"
                                            value="#{item}" />
                                        <h:graphicImage
                                            title="#{msgs.delete}"
                                            alt="#{msgs.delete}"
                                            url="/primefaces_resource/images/delete.png" />
                                    </h:commandLink>
                                </p>
                            </div>

.....................

and I need to catch some error and show a message to page, I wrote a code

public String delete() {
      try {
          actionFilter.delete(ReferenceDataType.class, selectedType.getTypeId());
      } catch (EJBTransactionRolledbackException e) {
          FacesContext.getCurrentInstance().addMessage("createForm:main_content",
                    new FacesMessage(FacesMessage.SEVERITY_ERROR, "error", null));
          return null;
        }
    return LIST;
  }

The error catched, but the page dont show a message. Please help.


Solution

  • This is what we do in our project:

    JSF:

    <t:messages id="messageId" showDetail="true" showSummary="false" layout="list" styleClass="errorMsg"" />
    

    JAVA:

    FacesContext context = FacesContext.getCurrentInstance();
    context.addMessage(null, new FacesMessage("ERROR MESSAGE"));
    return;
    

    What was the problem?

    The messages tag is incorrect, you had

    t:message
    

    and it is

    t:messages
    

    After checking the rest of configurations everything seems to work perfect with your code once you fix this tag.