我正在尝试在我的项目中实现登录系统,但还没有成功。我在我的项目中使用 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)
我做错了什么?:/