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>
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;
}
}
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>
I want it to appear only once when the user gets logged in correctly(If the else block is executed, it should not appear).
How can i achieve this?
Update
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;
}
}
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>