Reputation: 4572
I'm trying to implement a login system in my project but I can't get success yet. I'm using Tomcat 7, Java 6 and JSF 2.0 in my project, and always I try to redirect user to another page, I get a different error.
Following is the relevant part of 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>
My 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.");
}
}
Case 0: User authenticated, this is not the first time him access the website.
Case 1: user authenticated, this is the first time him access the website.
Case 2: not authenticated.
In my test, I have case 1. But, when I use "redirect", I got the following error message:
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)
And when I try to use "dispatch", I got:
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)
What I'm doing wrong? :/
Upvotes: 0
Views: 509
Reputation: 1646
I think you didn't use break statement inside switch so if after case 0 the case 1 will be executed and you will get the error "Cannot forward after response has been committed", try to return the url from this function,
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;
}
Upvotes: 3