Load modal only once

Asked

Viewed 100 times

-3

I have a modal that is loaded after login (automatic), but at each refresh the modal opens, I need it to open only once after logging in.

<rich:modalPanel id="modal" autosized="true" width="500" height="200" showWhenRendered="#{true}">
        <f:facet name="controls">
            <h:graphicImage value="/img/fechando.png" onclick="Richfaces.hideModalPanel('modalMensagem')"
                id="botaoFechar" />
        </f:facet>
        <f:facet name="header">
            <h:panelGroup>
                <h:outputText value="Olá" />
            </h:panelGroup>
        </f:facet>

        <h:outputText value="#{bean.mensagem}" escape="true" style="font-size:15px;"/>
    </rich:modalPanel>

tela2.xhtml

<ui:define name="conteudo">
    <h:form id="main">
        <rich:messages styleClass="msgerro" globalOnly="true" />

        <center style="font-size: 10pt;">
            Olá
            <br />
        </center>
    </h:form>

    <ui:include src="/modal/modalMensagem.xhtml"/>
</ui:define>

BEAN

public String getMensagem() {
    return MENSAGEM;
} 

Do I need a method to actually enter false to open only once? Since showWhenRendered by default is true.

  • If you want to open only once after the first login, you will have to write a flag saying you have already opened once. If you want to open the first time per session, you can set this flag as a session attribute and use it as a condition not to open the popup.

  • Is there an example for me to check how it does? Some tutorial...

  • If you do not want to write to the database, you can write to the user’s location.

  • Any example? Because I don’t know if creating a false setando method would solve...

1 answer

0


I would take the following approach:

<rich:modalPanel id="modal" autosized="true" width="500" height="200" showWhenRendered="#{homePageMB.showWelcomeModal()}">
    <f:facet name="controls">
        <h:graphicImage value="/img/fechando.png" onclick="Richfaces.hideModalPanel('modalMensagem')"
            id="botaoFechar" />
    </f:facet>
    <f:facet name="header">
        <h:panelGroup>
            <h:outputText value="Olá" />
        </h:panelGroup>
    </f:facet>
    <h:outputText value="#{bean.mensagem}" escape="true" style="font-size:15px;"/>
</rich:modalPanel>

And in your bean Managed (the one I gave the affectionately name of HomePageMB), you’d have a check like this:

public class HomePageMB {
    ...

    private static final String CHECKED_WELCOME_MODAL = "checkedWelcomeModal";

    public boolean showWelcomeModal() {
        Map<String, Object> sessionMap = FacesContext.getCurrentInstance().getExternalContext().getSessionMap();

        if(sessionMap.get(CHECKED_WELCOME_MODAL) == null) {
            sessionMap.put(CHECKED_WELCOME_MODAL, true);
            return true;
        }

        return false;
    }
    ...
}

With this if it is the first login, the flag will be null in the session, and after returning to the home page it will already have the flag in the session, and the modal will not be opened.

As I mentioned in the comment, there are other solutions to this problem, depends on your need.

  • Too good your help, but this way, the modal does not open...

  • I did not post a complete configuration of the bean Managed, you applied on one that is working on your project?

  • Yes, I did in your model implementing in my.

  • Came to debug to see if it’s returning true? If not being performed evaluate after screen rendering?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.