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

标签 java jsf jakarta-ee jsf-2 primefaces

我想使用一个名为通知栏的 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>

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

@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):

<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 block ,它不应该出现)。

我怎样才能做到这一点?

更新

更改了 logIn() 方法:

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 添加这个

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

最佳答案

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

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

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

关于java - 如何在登录时显示带有欢迎消息的通知?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5730054/

相关文章:

java - 服务器抛出 OptimisticLockException 后事务没有完全回滚

java - 使用 JSCH (android) 将 ssh 日志打印到列表

java - JSF 的最佳实践是什么?

java - 如何使用 Spring Security 将 'Remember me' 复选框引入现有项目?

java - (已修订)Eclipse EE 中 JSP 项目(使用 Tomcat 7)的根文件夹路径

multithreading - JSF 中的线程?

java - Struts2 jquery 选项卡式面板超时不触发事件

java - 使用 java8 流构建集合

java - eclipse中突然出现ClassNotFoundException

eclipse - NLS缺少消息:CANNOT_FIND_FACELET_TAGLIB