【发布时间】:2014-12-26 04:57:51
【问题描述】:
我一直在努力创建一个页面来确认注册,然后将用户重定向到 index.xhtml,但不幸的是没有工作。我已经尝试调试,但控制台没有显示任何内容。
我要做的是在页面加载后使用 f:event preRenderView 调用一个方法。此方法将更新用户以激活用户,然后重定向到索引。 UPDATE 方法有效,但我无法重定向用户。
我创建了一个普通按钮来重定向并且它有效,但我不希望我的用户点击任何东西 =(
你们能帮帮我吗?
这是我的 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>
这是我的确认 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;
}
}
【问题讨论】: