I have a little problem when I'am trying to redirect page. This is my code:
public String doLoginn(){
try{
Users users = DaoFactory.getUsersDao().getUsersByUsernameAndPasswordAndRole(username, password, id_role_fk);
if(users != null){
FacesContext context = FacesContext.getCurrentInstance();
HttpSession session = (HttpSession) context.getExternalContext().getSession(true);
RequestContext rc = RequestContext.getCurrentInstance();
session.setAttribute("LOGIN USER", users);
LOGGER.info("Login success");
if(users.getId_role_fk() == 2)
{
rc.execute("swal('Login success!', 'Congratulations! you are logged in as Management', 'success')");
return "home?faces-redirect=true";
} else{
rc.execute("swal('Login success!', 'Congratulations! you are logged in as Admin', 'success')");
return "list-data-user?faces-redirect=true";
}
} else{
LOGGER.error("Login failed");
AppBean.addErrorMsg("Invalid Username and Password");
}
} catch(SQLException e){
AppBean.addErrorMsg("Cannot execute db");
LOGGER.error("Failed to execute db: " + e.getCause());
}
return null;
}
So that is a login function. When the login was succeed there would be a sweet alert and if I click 'OK' it suppose to redirect to another page. But, I can't find anyway to do it. I set my sweet alert in the bean, so in my xhtml I just call the doLoginn function. Do you have any suggestion? Here is my xhtml code for button login:
<!-- button login -->
<div class="col s12 input-field" style="margin-left: 155px;">
<h:commandLink value="Login" styleClass="btn btn-large"
actionListener="#{loginBean.doLoginn()}" />
</div>
Base on my bean, if I use action it will redirect page but not showing the sweet alert and if I use actionListener it will show the sweet alert but not redirect page.