-1

I've been trying really hard to create a page to confirm a registration and then redirect the user to the index.xhtml, but unfortunately is not working. I tried to debug already but the console doesn't show anything.

What I'm trying to do is to call a method after the page loads using f:event preRenderView. This method will UPDATE the User to activate the User and then redirect to the index. The UPDATE method is working, but I CANT REDIRECT THE USER.

I created a normal button to redirect and it worked, but I wouldn't like that my user click anything =(

Could you guys help me ?

here is my index.xhtml

<ui:composition template="/template/template.xhtml"
    xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://xmlns.jcp.org/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:p="http://primefaces.org/ui"
    xmlns:pt="http://xmlns.jcp.org/jsf/passthrough">


    <ui:define name="title">EmaiL</ui:define>
    <ui:define name="subtitle">Confirmação de Email</ui:define>


    <ui:define name="parametros">
        <f:metadata>
            <f:viewParam name="email" value="#{confirmation.email}"></f:viewParam>
            <f:viewParam name="hash" value="#{confirmation.hash}"></f:viewParam>
        </f:metadata>

    </ui:define>

    <ui:define name="corpo">

        <h:form>
            <f:event listener="#{confirmation.confirm}" type="preRenderView"></f:event>
            My email is: #{confirmation.getEmail()}
            My hash is: #{confirmation.getHash()}
        </h:form>
    </ui:define>

</ui:composition> 

And here is my confirmation bean:

package br.com.cesar.primeirodrop.beans;

import java.io.Serializable;

    import javax.annotation.ManagedBean;
    import javax.faces.bean.ViewScoped;
    import javax.faces.context.FacesContext;
    import javax.faces.context.Flash;

    import org.springframework.beans.factory.annotation.Autowired;

    import br.com.cesar.primeirodrop.services.AlunoService;
    import br.com.cesar.primeirodrop.util.FacesUtil;

        @ManagedBean(value = "confirmation")
        @ViewScoped
        public class Confirmation implements Serializable {

            /**
             * 
             */
            private static final long serialVersionUID = 1L;

            private String email;
            private String hash;

            private AlunoService service;

            @Autowired
            public Confirmation(AlunoService service) {
                // TODO Auto-generated constructor stub
                this.service = service;
            }

            public String getEmail() {
                return email;
            }
            public void setEmail(String email) {
                this.email = email;
            }
            public String getHash() {
                return hash;
            }
            public void setHash(String hash) {
                this.hash = hash;
            }


  public void confirm() {

    Flash flash = FacesContext.getCurrentInstance().getExternalContext()
            .getFlash();
    flash.setKeepMessages(true);

    String url = "/PrimeiroDrop/index.xhtml";
    FacesContext fc = FacesContext.getCurrentInstance();
    ExternalContext ec = fc.getExternalContext();

    if (service.ConfirmaCadastro(this.email, this.hash)) {

        try {
            ec.redirect(url);
            FacesUtil
                    .adicionaMsgDeSucesso("Seu Cadastro Foi Confirmado com sucesso, porfavor log in!");
        } catch (Exception e) {
            // TODO: handle exception
        }

    } else {

        try {
            ec.redirect(url);
            FacesUtil
                    .adicionaMsgDeErro("Usuário não encontrado na nossa base de dados");
        } catch (Exception e) {
            // TODO: handle exception
        }
    }
}
            @Override
            public int hashCode() {
                final int prime = 31;
                int result = 1;
                result = prime * result + ((email == null) ? 0 : email.hashCode());
                result = prime * result + ((hash == null) ? 0 : hash.hashCode());
                return result;
            }
            @Override
            public boolean equals(Object obj) {
                if (this == obj)
                    return true;
                if (obj == null)
                    return false;
                if (getClass() != obj.getClass())
                    return false;
                Confirmation other = (Confirmation) obj;
                if (email == null) {
                    if (other.email != null)
                        return false;
                } else if (!email.equals(other.email))
                    return false;
                if (hash == null) {
                    if (other.hash != null)
                        return false;
                } else if (!hash.equals(other.hash))
                    return false;
                return true;
            }
        }

1 Answers1

0

IMHO, the reason it works with the button is because you call the method as action, which handles the return value as navigation rule. <f:event> listener does not do that. Try this code for redirection (taken from this answer)

String url = (something)
FacesContext fc = FacesContext.getCurrentInstance();
ExternalContext ec = fc.getExternalContext();
try {
        ec.redirect(url);
} catch (IOException ex) {
        Logger.getLogger(Navigation.class.getName()).log(Level.SEVERE, null, ex);
}
Community
  • 1
  • 1
Predrag Maric
  • 23,938
  • 5
  • 52
  • 68
  • Hey Predrag Maric, thank you man, now it is redirecting but FacesMessage is not being passed. Even using flash =( – user3915709 Oct 30 '14 at 10:23
  • That's because of redirect. You could put the message in the session, and when the other page loads, check if there are any messages in the session that need to be displayed. – Predrag Maric Oct 30 '14 at 10:26
  • @user3915709 - How are you using flash? Did you set `keepMessages(true)`? – kolossus Oct 30 '14 at 16:10
  • 1
    That's a poor recommendation; a misuse of the session scope @PredragMaric. – kolossus Oct 30 '14 at 16:11
  • @kolossus I've seen a number of questions about this, where `keepMessages(true)` doesn't work. IMHO, this is a decent workaround when one wants to display messages after redirect. – Predrag Maric Oct 30 '14 at 16:13