gpt4 book ai didi

java - 无法重定向或分派(dispatch)到 JSF 2.0 中的另一个页面

转载 作者:行者123 更新时间:2023-12-01 23:26:41 25 4
gpt4 key购买 nike

我正在尝试在我的项目中实现登录系统,但尚未成功。我在我的项目中使用 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。但是,当我使用“重定向”时,我收到以下错误消息:

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)

我做错了什么? :/

最佳答案

我认为你没有在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;
}

关于java - 无法重定向或分派(dispatch)到 JSF 2.0 中的另一个页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19875614/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com