0

I am having a strange issue with the Faces messages.

Sometimes, the <p:growl>, <p:messages> display correctly the notification after executing some method. But there are specific JSF pages where no notification is shown.

For example:

public String create() {
    try {
        getFacade().create(current);
        JsfUtil.addSuccessMessage(ResourceBundle.getBundle("/Bundle").getString("NotaCreated"));
        return prepareCreate();
    } catch (Exception e) {
        JsfUtil.addErrorMessage(e, ResourceBundle.getBundle("/Bundle").getString("PersistenceErrorOccured"));
        return null;
    }
}

The method is called in

<h:commandLink action="#{notaController.create}" value="#{bundle.CreateNotaSaveLink}"/>

And it does not show the notification. But for other entities as well as for other operations the messages are displayed correctly. This happens also in other functions, where I need feedback and it is not shown as expected.

I've been searching around and read this post and tried some of the suggested solutions, including setting the ajax=false attribute in commandButtons and commandLinks or using the scripts posted, but none of that worked for me, so I ask this question hoping that someone can help me.

Do I have to configure something to make it work correctly? I need this to get fixed by tomorrow and I don't know what else to do.

Update:

These are the JsfUtil methods:

public static void addSuccessMessage(String msg) {
    FacesMessage facesMsg = new FacesMessage(FacesMessage.SEVERITY_INFO, msg, msg);
    FacesContext.getCurrentInstance().addMessage("successInfo", facesMsg);
}
public static void addErrorMessage(Exception ex, String defaultMsg) {
    String msg = ex.getLocalizedMessage();
    if (msg != null && msg.length() > 0) {
        addErrorMessage(msg);
    } else {
        addErrorMessage(defaultMsg);
    }
}

This is the Create.xhtml

<html xmlns="http://www.w3.org/1999/xhtml"
  xmlns:ui="http://java.sun.com/jsf/facelets"
  xmlns:h="http://java.sun.com/jsf/html"
  xmlns:f="http://java.sun.com/jsf/core"
  xmlns:p="http://primefaces.org/ui">

<ui:composition template="/template.xhtml">
    <ui:define name="title">
        <h:outputText value="#{bundle.CreateNotaTitle}"></h:outputText>
    </ui:define>
    <ui:define name="body">
        <h:panelGroup id="messagePanel" layout="block">
            <h:messages errorStyle="color: red" infoStyle="color: green" layout="table"/>
        </h:panelGroup>
        <h:form>
            <h:panelGrid columns="2">

                <h:outputLabel value="#{bundle.CreateNotaLabel_fecha}" for="fecha" />
                <p:calendar value="#{notaController.selected.fecha}" id="fecha"/>
                <h:outputLabel value="#{bundle.CreateNotaLabel_observaciones}" for="observaciones" />
                <p:inputTextarea rows="4" cols="30" id="observaciones" value="#{notaController.selected.observaciones}" title="#{bundle.CreateNotaTitle_observaciones}" />
                <h:outputLabel value="#{bundle.CreateNotaLabel_tipoNota}" for="tipoNota" />
                <p:selectOneMenu id="tipoNota" value="#{notaController.selected.tipoNota}" required="true" requiredMessage="#{bundle.CreateNotaRequiredMessage_tipoNota}">
                    <f:selectItem itemLabel="---"/>
                    <f:selectItem itemValue="debito" itemLabel="Débito"/>
                    <f:selectItem itemValue="credito" itemLabel="Crédito"/>
                </p:selectOneMenu>
                <h:outputLabel value="#{bundle.CreateNotaLabel_monto}" for="monto" />
                <p:inputText id="monto" value="#{notaController.selected.monto}" title="#{bundle.CreateNotaTitle_monto}" required="true" requiredMessage="#{bundle.CreateNotaRequiredMessage_monto}"/>
                <h:outputLabel value="#{bundle.CreateNotaLabel_factura}" for="factura" />
                <p:selectOneMenu id="factura" converter="#{facturaController.convertidor}" value="#{notaController.selected.factura}" filter="true" filterMatchMode="startsWith" required="true" requiredMessage="#{bundle.CreateNotaRequiredMessage_factura}">
                    <f:selectItem itemLabel="---"/>
                    <f:selectItems value="#{facturaController.facturas}" var="factura" itemValue="#{factura}" itemLabel="#{factura.idFactura}" />
                </p:selectOneMenu>

            </h:panelGrid>
            <br />
            <h:commandLink action="#{notaController.create}" value="#{bundle.CreateNotaSaveLink}">
                <f:setPropertyActionListener value="#{cargaController.crearCargaDefault(sesionMB.usuario)}"
                                             target="#{notaController.selected.carga}"/>
            </h:commandLink>
            <br />
            <br />
            <h:commandLink action="#{notaController.prepareList}" value="#{bundle.CreateNotaShowAllLink}" immediate="true"/>
            <br />
            <br />
            <h:link outcome="/inicio" value="#{bundle.CreateNotaIndexLink}"/>
        </h:form>
    </ui:define>
</ui:composition>

Community
  • 1
  • 1
Carlos Andres
  • 69
  • 1
  • 12
  • Your question is missing anything that could be used to help troubleshoot your issue. Where is the view? What's `JSFUtil` and is it working properly? – kolossus Aug 03 '14 at 16:06
  • @JaqenH'ghar I have added the methods for messaging. I don't know any other code example to give because all controllers have the same create() method and for some of them it works but for this and others it does not. – Carlos Andres Aug 03 '14 at 16:49
  • @kolossus I have added the code for the create view. As I said before, JsfUtil method work for some methods, but not for others. In any method where I call the JsfUtil.addSuccessMessage(message) sometimes the message is displayed and sometimes it doesn't – Carlos Andres Aug 03 '14 at 16:54
  • @JaqenH'ghar yes, no errors or warnings – Carlos Andres Aug 03 '14 at 17:37
  • Thanks for your colaboration, I checked my code and found that the problem was only when the return strings had ?faces-redirect=true. I removed that and it worked just fine. But the problem is that I need the redirected link as a requirement. How can I deal with that without having this issue? – Carlos Andres Aug 05 '14 at 06:25

1 Answers1

1

I had the same problem. The faces message wasn't rendering it was being lost. I tried this code and it worked.

FacesMessage facesMsg = new FacesMessage(FacesMessage.SEVERITY_INFO, msg, msg);
FacesContext context = FacesContext.getCurrentInstance();
context.getExternalContext().getFlash().setKeepMessages(true);
context.addMessage("successInfo", facesMsg);

I hope it works.

pamps
  • 304
  • 6
  • 17
  • I did tried that before posting this. I was getting a warning console message about the messages, don't remember, but found out that the problem is when you put in the return strings ?faces-redirect=true – Carlos Andres Aug 05 '14 at 06:20
  • @Carlos Andres Read the accepted answer in this link [link]http://stackoverflow.com/questions/13685633/how-to-show-faces-message-in-the-redirected-page BalusC talks about a bug in messages when the page is redirected. Maybe it's your case. – pamps Aug 05 '14 at 11:47