我在Usuario usuario = gestionUsuario.hacerLogin(nombre, clave);
上有一个例外,我希望捕获异常并在上下文消息中显示它。我实现了下一个代码,但应用程序显示了一条警告消息:serverError: class javax.faces.el.EvaluationException
public String iniciarSesion() throws IOException {
try{
GestionUsuario gestionUsuario = new GestionUsuario();
Usuario usuario = gestionUsuario.hacerLogin(nombre, clave);
}catch(EvaluationException ex){
FacesMessage message = new FacesMessage(ex.toString());
FacesContext context = FacesContext.getCurrentInstance();
context.addMessage(btnLogin.getClientId(context), message);
return null;
}
FacesContext.getCurrentInstance().getExternalContext().redirect("inicio.xhtml");
return "inicio";
}
XHTML。 (按钮调用的动作iniciarSesion
)
<h:form id = "loginForm">
<h:outputLabel id = "txtUsuario" for = "inputUsuario" value = "#{msgs['loginForm.usuario']}"/>
<b:inputText id = "inputUsuario" required = "true" value = "#{logBean.nombre}" class = "inputText"/>
<br/>
<h:outputLabel id = "txtClave" for = "inputClave" value = "#{msgs['loginForm.clave']}"/>
<b:inputSecret id = "inputClave" required = "true" value = "#{logBean.clave}" class = "inputPassword"/>
<br/>
<div class="row-fluid">
<div class="span10 text-center">
<h:message for="btnLogin" class = "text-danger"/>
</div>
</div>
<br/>
<b:commandButton id = "btnLogin" class = "btn btn-success center-block" value = "#{msgs['loginForm.boton']}" action = "#{logBean.iniciarSesion}" update = "@form"/>
</h:form>
我发现:上下文是空的。
context = FacesContext.getCurrentInstance();
输出异常:
Caused by: java.lang.NullPointerException
at beans.backing.LogBean.iniciarSesion(LogBean.java:52)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.sun.el.parser.AstValue.invoke(AstValue.java:289)
at com.sun.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:304)
at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105)
at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:87)
... 36 more