无法重定向或分派到JSF 2.0中的其他页面

时间:2013-11-09 11:53:37

标签: java jsf

我正在尝试在我的项目中实现登录系统,但我还没有成功。我在我的项目中使用Tomcat 7,Java 6和JSF 2.0,并且总是尝试将用户重定向到另一个页面,我得到了一个不同的错误。

以下是login.xhtml的相关部分:

<h:form id="form-login">
    <fieldset>
    <div class="form-group">
        <label for="login">Login:</label>
        <p:inputText styleClass="form-control" autocomplete="off" id="login" 
        placeholder="Nome de usuário" style="padding: 6px 12px;" required="true" 
        requiredMessage="Este campo é obrigatório." value="#{loginMBean.login}"/>
    </div>

    <div class="form-group">
        <label for="senha">Senha: </label>
        <p:inputText styleClass="form-control" autocomplete="off" id="senha"                                
                type="password" placeholder="Senha" style="padding: 6px 12px;" 
                required="true" requiredMessage="Este campo é obrigatório." 
                value="#{loginMBean.password}" />
    </div>

    <div class="form-actions">
        <h:commandButton value="Entrar" styleClass="btn btn-default btn-lg" 
                action="#{loginMBean.entrar}" />
    </div>
    </fieldset>
</h:form>

我的loginMBean

(String login, string password, this part is okay, already works)
...

public void entrar() throws IOException{

    Login loginTentativa = new Login(login, password);

    int resultadoValidacao = controle.validaLogin(loginTentativa);

    switch (resultadoValidacao) {
        case 0:
               FacesContext.getCurrentInstance().getExternalContext().dispatch("first/1-dados-escola.xhtml");
        case 1:                         
               FacesContext.getCurrentInstance().getExternalContext().redirect("/user/principal.xhtml");

        default:
            System.out.println("Usuário não autenticado.");
    }
}

案例0:用户通过身份验证,这不是他第一次访问该网站 案例1:用户通过身份验证,这是他第一次访问该网站 案例2:未经过身份验证。

在我的测试中,我有案例1.但是,当我使用“redirect”时,我收到以下错误消息:

javax.faces.el.EvaluationException: java.lang.IllegalStateException
    javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:101)
    com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:101)
    javax.faces.component.UICommand.broadcast(UICommand.java:315)
    javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:791)
    javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1256)
    com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
    com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
    javax.faces.webapp.FacesServlet.service(FacesServlet.java:593) 

当我尝试使用“dispatch”时,我得到了:

 javax.faces.el.EvaluationException: java.lang.IllegalStateException: Cannot forward after response has been committed
    javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:101)
    com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:101)
    javax.faces.component.UICommand.broadcast(UICommand.java:315)
    javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:791)
    javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1256)
    com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
    com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
    javax.faces.webapp.FacesServlet.service(FacesServlet.java:593)

我做错了什么? :/

1 个答案:

答案 0 :(得分:3)

我认为你没有在switch中使用break语句,所以如果在case 0之后将执行case 1并且你将得到错误“在响应提交后无法转发”,尝试从该函数返回url,

 public String entrar() throws IOException{

Login loginTentativa = new Login(login, password);
 String result="";

int resultadoValidacao = controle.validaLogin(loginTentativa);

switch (resultadoValidacao) {
    case 0:
           result="first/1-dados-escola.xhtml";
    break;
    case 1:                         
           result="/user/principal.xhtml";
    break;

    default:
        System.out.println("Usuário não autenticado.");
}

return result;
 }