gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:01:51 25 4
gpt4 key购买 nike

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com