0

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.

Kukeltje
  • 12,223
  • 4
  • 24
  • 47
Leopards
  • 13
  • 5
  • 1
    so if you change `swal` to `alert` and concatenate the strings you do see an alert? And this is not PrimeFaces related... I removed that tag. Oh and I have a 'deja-vu'... Can that be correct? – Kukeltje Mar 13 '20 at 08:47
  • @Kukeltje yes I'm the same person bro. I realize that the way I asking in my last question is hard to understand so I change it. Hope it will be a better way for people to understand. I'am sorry if I offend you.. – Leopards Mar 13 '20 at 08:52
  • You did not offend me... But I cannot see the other question anymore. So that surprised me. And it is not really hard to understand but you should narrow things down. That is what my first comment is about. This is in no way sweetalert related. – Kukeltje Mar 13 '20 at 09:12
  • Your title might be 'confusing'... You don't redirect to another page with with (by means of) sweetalert, you want to redirect to another page that contains/runs the sweetalert. Then you can simplify and see if running something else (a plain alert) DOES work... If it does not, you narrowed things doen that sweetalert is not related and you can search in a more generic way: "How to execute javascript from the serverside in a page I'm redirected to" – Kukeltje Mar 13 '20 at 09:17
  • @Kukeltje okay I will try search it. Thanks for the advice – Leopards Mar 13 '20 at 09:21
  • 2
    As explained in your deleted question, your real question is basically "How do I execute JavaScript code on redirected page after specific JSF action?". – BalusC Mar 13 '20 at 10:39
  • Does this answer your question? [Calling a JavaScript function from managed bean](https://stackoverflow.com/questions/5675017/calling-a-javascript-function-from-managed-bean) – fuggerjaki61 Mar 13 '20 at 16:40
  • @fuggerjaki61: no it does not. It does not address the ussie balusC and me mentioned in the comments... execute javascript **after** a redirect – Kukeltje Mar 13 '20 at 22:31

0 Answers0