Hi,
I want to show a message to the user when he enter the application. But the message should be shown only once per session.
What is the best way to do that?
thanks a lot
Hi,
I want to show a message to the user when he enter the application. But the message should be shown only once per session.
What is the best way to do that?
thanks a lot
One way is to store a boolean flag in VaadinSession
that indicates whether or not the message was shown to the user. Then add a UIInitListener
that would check this boolean flag and display the message if appropriate.
Here is the code in a Vaadin + Spring project:
@SpringComponent
public class ApplicationServiceInitListener implements VaadinServiceInitListener {
@Override
public void serviceInit(ServiceInitEvent serviceInitEvent) {
serviceInitEvent.getSource().addSessionInitListener(initEvent -> {
initEvent.getSession().setAttribute("isMsgDisplayed", Boolean.FALSE);
});
serviceInitEvent.getSource().addUIInitListener(event -> {
VaadinSession currSession = VaadinSession.getCurrent();
if (!(boolean) currSession.getAttribute("isMsgDisplayed")) {
UI ui = event.getUI();
ui.addBeforeEnterListener(beforeEvent -> {
Dialog d = new Dialog(new Span("A one time message"));
d.open();
});
currSession.setAttribute("isMsgDisplayed", Boolean.TRUE);
}
});
}
}
In a plain Java project (non-Spring), this class should be registered as a provider via Java SPI loading facility as described here: https://vaadin.com/docs/v14/flow/advanced/tutorial-service-init-listener.html