无法使用f:event preRenderView重定向

时间:2014-10-30 08:24:41

标签: java spring jsf-2

我一直在努力创建一个页面来确认注册,然后将用户重定向到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> 

这是我的确认豆:

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 个答案:

答案 0 :(得分:0)

恕我直言,它使用该按钮的原因是因为您将该方法称为操作,它将返回值作为导航规则处理。 <f:event>听众不这样做。尝试使用此代码进行重定向(取自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);
}