【问题标题】:Can't Redirect Using f:event preRenderView无法使用 f:event preRenderView 重定向
【发布时间】: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;
            }
        }

【问题讨论】:

    标签: java spring jsf-2


    【解决方案1】:

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

    【讨论】:

    • 嘿 Predrag Maric,谢谢你,现在它正在重定向,但 FacesMessage 没有被传递。即使使用闪光灯 =(
    • 那是因为重定向。您可以将消息放入会话中,当其他页面加载时,检查会话中是否有需要显示的消息。
    • @user3915709 - 你是如何使用闪存的?你设置keepMessages(true)了吗?
    • 这是一个糟糕的建议;滥用会话范围@PredragMaric。
    • @kolossus 我已经看到了很多关于这个的问题,其中keepMessages(true) 不起作用。恕我直言,当一个人想要在重定向后显示消息时,这是一个不错的解决方法。
    猜你喜欢
    • 2011-12-30
    • 2012-03-17
    • 2012-01-20
    • 1970-01-01
    • 1970-01-01
    • 2010-10-31
    • 2020-12-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多