Java 如何在登录时显示带有欢迎消息的通知?

Java 如何在登录时显示带有欢迎消息的通知?,java,jsf,jakarta-ee,jsf-2,primefaces,Java,Jsf,Jakarta Ee,Jsf 2,Primefaces,我想使用一个名为Notification bar的primefaces工具,在用户登录时显示一条表示欢迎的消息。问题是,我不知道如何触发它,只有当登录成功时(如果不应该显示错误的密码),并且即使我被重定向到另一个页面也会显示它 这是我的日志页面的外观: <ui:composition template="WEB-INF/templates/BasicTemplate.xhtml"> <!-- THE REGISTRATION FORM --> <ui

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

这是我的日志页面的外观:

<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>
通知栏位于所有页面都使用的模板中(BasicTemplate.xhtml):

在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>

主页
jQuery(函数(){
topBar.show()
});

在main.xhtml中包含
p:notificationBar
(将其从模板中删除),并将以下javascript添加到main.xhtml中:


jQuery(函数(){topBar.show()});

这将在页面加载时显示栏。如果我没有看错您的代码,main.xhtml只有在成功登录后才会显示。

如果您想在main.xhtml中显示通知栏,我很困惑@sfrj为什么会在登录页面中显示?是的,您是对的。我只是改变了这一点(见更新的问题)。通知栏当前位于所有页面正在使用的模板中。如果登录正确,如何从登录按钮触发它?我认为问题是在加载main.xhtml时触发该条。由于OP使用jsf模板,因此页面上没有
h:body
可以添加
onLoad=“topBar.show()”
是否应该将onLoad添加到BasicTemplate.xhtml。如果是这样的话,我如何控制它在单击登录按钮时显示?在加载main.xhtml时触发该条不是很好吗?还是在登录失败后加载?[<5分钟后]:好的,我从代码中看到,登录失败后返回一个空字符串。(可能不相关,但您应该返回null以便重新加载)Main.xhtml不仅在成功登录时显示,在没有登录时也会显示(未登录的用户可以使用导航栏导航到它并查看它)。我需要确保该栏只在用户登录时出现,并且只出现一次。我们是否可以对该脚本进行更多的调整以使其按需要工作?您可以使用通知栏的
rendered
属性,并让呈现取决于用户是否登录。只是个主意。(这有点脏,因为如果不呈现组件,会导致js错误)。我想我会尝试类似的想法。我想我将在会话中保存一些变量,然后仅当该变量不为null时才进行渲染。之后,我将从会话中删除该变量。我认为这可能奏效,而且我认为这只会显示一次。我不知道这是否可能,但我会尝试。另一个选项是让您的登录方法返回main.xhtml和url参数,例如
return“main.xhtml?faces redirect=true&login=1
,然后获取此参数。查看您是否想知道如何在jsf页面中获取请求参数
<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>
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;
        }
    }
 <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>
<script type="text/javascript">
     jQuery(function() { topBar.show() });
</script>