How to display notification with welcome message when logged in?

心不动则不痛 提交于 2019-12-21 20:23:24

问题


I want to use a primefaces tool called Notification bar to display a message that says welcome, when the user logs in. The problem is that i don't know how to trigger it, only if the login is successful(if wrong password should not be displayed) and also to be displayed even if i am redirected to another page.

This is how my loggin page looks like:

<ui:composition template="WEB-INF/templates/BasicTemplate.xhtml">
    <!-- THE REGISTRATION FORM -->
    <ui:define name="loginForm">
       <h2>Login page</h2>
       <h:form>
       <p:panel>                
                    <h:outputText value="*Em@il:" />
                    <h:inputText id="email" value="#{securityController.email}" binding="#{emailComponent}"/>                   
                    <br/>
                    <h:outputText value="*Lozinka: " />
                    <h:inputSecret id="password" value="#{securityController.password}" validator="#{securityController.validate}">                     
                        <f:attribute name="emailComponent" value="#{emailComponent}" />
                    </h:inputSecret>            

                    <br/>
                    <span style="color: red;"><h:message for="password"
                    showDetail="true" /></span> 
                    <br/>
                    <h:commandButton value="Login" action="#{securityController.logIn()}" onclick="topBar.show()"/>                 

                </p:panel>
            </h:form>   

    </ui:define>

</ui:composition>

This is the method of the managed bean that does the redirection:

@ManagedBean
@RequestScoped
public class SecurityController {

    @EJB
    private IAuthentificationEJB authentificationEJB;       

    public String logIn() {     
        if (authentificationEJB.saveUserState(email, password)) {               
            return "main.xhtml";
        } else {
            return null;
        }
    }

The notification bar is located in a Template that all the pages use(BasicTemplate.xhtml):

<f:view contentType="text/html">
    <h:head>
        ...
    </h:head>

    <h:body>
        ...     
        <p:notificationBar position="top" widgetVar="topBar" styleClass="top">
            <h:outputText value="Welcome, you are now logged in!"
                style="color:#FFCC00;font-size:36px;" />
        </p:notificationBar>
    </h:body>
</f:view>

I want it to appear only once when the user gets logged in correctly(If the else block is executed, it should not appear).

How can i achieve this?

Update

changed the logIn() method:

public String logIn() {
        if (authentificationEJB.saveUserState(email, password)) {
            // Pass a parameter in ussing the URL.(The notification bar will
            // read this parameter)
            return "main.xhtml?faces-redirect=true&login=1";
        } else {
            return null;
        }
    }

Added this at main.xhtml

 <ui:composition template="WEB-INF/templates/BasicTemplate.xhtml">
    <ui:define name="mainForm">      
        <h2>The main page</h2>
        <script type="text/javascript">
          jQuery(function() {
          topBar.show()
           });
         </script>          
        <p:notificationBar id="notbar" position="top" widgetVar="topBar" styleClass="top" rendered="#{param.login == 1}">
            <h:outputText value="Welcome, you are now logged in!"
                style="color:#FFCC00;font-size:36px;" />
        </p:notificationBar>

    </ui:define>

</ui:composition>

回答1:


Include the p:notificationBar in your main.xhtml (remove it from the template) and add the following javascript to your main.xhtml:

<script type="text/javascript">
     jQuery(function() { topBar.show() });
</script>

This will show the bar when the page loads. If I read your code right, main.xhtml is only shown after successful login.



来源:https://stackoverflow.com/questions/5730054/how-to-display-notification-with-welcome-message-when-logged-in

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!