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;
}
}