如何在登录时显示带欢迎消息的通知? [英] How to display notification with welcome message when logged in?

查看:119
本文介绍了如何在登录时显示带欢迎消息的通知?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用名为Notification bar的primefaces工具在用户登录时显示欢迎消息。问题是我不知道如何触发它,只有登录成功(如果错误)密码不应显示),即使我被重定向到另一页也要显示。

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>

这是进行重定向的托管bean的方法:

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;
        }
    }

通知栏位于模板中,全部是页面使用(BasicTemplate.xhtml):

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>

我希望它只在用户登录正确时出现一次(如果执行了else块,则不应该出现。)

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

我如何实现这一目标?

更新

更改了logIn()方法:

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;
        }
    }

在main.xhtml添加了这个

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>


推荐答案

包括 p:notificationBar main.xhtml 中的(从模板中删除)并将以下javascript添加到 main.xhtml

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

这将显示页面加载时的栏。如果我正确阅读您的代码, main.xhtml 仅在成功登录后显示。

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

这篇关于如何在登录时显示带欢迎消息的通知?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆