我想使用一个名为“通知栏”的 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块,则不应出现)。
我怎样才能做到这一点?
Update
更改了 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>