How to display a notification message with a welcome message when logging in?

I want to use the editing tool called the notification bar to display a message that is welcome when a user logs in. The problem is that I don’t know how to start it, only if the login is completed successfully (if the password should not be displayed incorrectly), and also displayed even if I was redirected to another page.

This is what 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 a managed bean method that redirects:

 @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 panel is located in the template that is used by all pages (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 be displayed only once when the user logs 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 to 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> 
+3
source share
1 answer

Include p:notificationBar in your main.xhtml file (remove it from the template) and add the following javascript to your main.xhtml file:

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

This will show the panel when the page loads. If I read your code correctly, main.xhtml will only be shown after a successful login.

+3
source

Source: https://habr.com/ru/post/974347/


All Articles