JSF 1.x通用异常处理

时间:2009-01-20 09:37:58

标签: jsp jsf exception-handling jsf-1.2

如果我的业务层中有异常(例如我的JDBC连接bean中的SQL异常),我如何将自定义消息传播到全局error.jsp页面?

6 个答案:

答案 0 :(得分:19)

JSF 1.x不提供此类型的任何隐式错误处理,但您可以使用导航规则重定向到错误页面(假设表单发布)...

<navigation-case>
<description>
Handle a generic error outcome that might be returned
by any application Action.
</description>
<display-name>Generic Error Outcome</display-name>
<from-outcome>loginRequired</from-outcome>
<to-view-id>/must-login-first.jsp</to-view-id>
</navigation-case>

...或使用重定向...

FacesContext context = FacesContext.getCurrentInstance();
ExternalContext extContext = context.getExternalContext();
String url = extContext.encodeActionURL(extContext
        .getRequestContextPath()
        + "/messages.faces");
extContext.redirect(url);

我建议您查看JSF specification了解详情。

可以根据需要将错误消息放在请求范围/会话范围/ url参数上。


假设有一个Servlet容器,您可以使用通常的web.xml error page configuration

<error-page>
    <exception-type>java.lang.Exception</exception-type>
    <location>/errorPage.faces</location>
</error-page>

在您的支持bean中,您可以在RuntimeException s中包装并抛出已检查的异常。

一些JSF实现/框架将捕获这些错误(Apache MyFaces / Facelets),因此您必须configure them not to

答案 1 :(得分:8)

答案 2 :(得分:6)

我在网站面板中创建了一个错误页面并放置了错误的堆栈跟踪 此代码首先将其放在web.xml中

<error-page>
                <error-code>500</error-code>
                <location>/errorpage.jsf</location>
</error-page>

虽然jsf页面有这段代码

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"   
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      >
<h:head>
    <h:outputStylesheet name="EstilosOV.css" library="css" />
    <!-- <h:outputScript name="sincontext.js" library="scripts"/> -->

</h:head>
<h:body >
<table width="338" border="0" align="center" bordercolor="#FFFFFF">

    <tr bgcolor="#1D2F68">
      <td colspan="4" height="35" class="TablaHeader2"> PLAN SEGURO</td>
    </tr>
    <tr  bgcolor="#FDF2AA">
    <td colspan="4"  class="HeaderExcepcion">
      <h:graphicImage library="images" name="errormark.jpg"/>
      <font size="5px" color="#CC0000" >Error de ejecución</font>
      </td>
    </tr>
    <tr >
    <td colspan="3" class="anuncioWarning2">Ocurrio un error al realizar la operación, favor de intentarlo nuevamente, si el error aún se presenta, favor de notificarlo a soporte técnico al 5147-31-00 extensión 6893</td>
    </tr>
    <tr>
      <td height="17" colspan="3" class="TablaHeader2"></td>
    </tr>

</table>
    <h:form id="formerror">
        <h:inputHidden id="valuestack" value="#{errorDisplay.stackTrace}"></h:inputHidden>
    </h:form>
</h:body>

</html>

请注意,motif允许您调试一个reazon的堆栈跟踪,知道哪个是隐藏字段中的例外。 让我们看一下如何定义类恢复StackTrace

public class ErrorDisplay implements Serializable{

    private static final long serialVersionUID = 6032693999191928654L;
    public static Logger LOG=Logger.getLogger(ErrorDisplay.class);

    public String getContacto() {
        return "";
    }

    public String getStackTrace() {
        FacesContext context = FacesContext.getCurrentInstance();
        Map requestMap = context.getExternalContext().getRequestMap();
        Throwable ex = (Throwable) requestMap.get("javax.servlet.error.exception");

        StringWriter writer = new StringWriter();
        PrintWriter pw = new PrintWriter(writer);
        fillStackTrace(ex, pw);
        LOG.fatal(writer.toString());

        return writer.toString();
    }

    private void fillStackTrace(Throwable ex, PrintWriter pw) {
        if (null == ex) {
            return;
        }

        ex.printStackTrace(pw);

        if (ex instanceof ServletException) {
            Throwable cause = ((ServletException) ex).getRootCause();

            if (null != cause) {
                pw.println("Root Cause:");
                fillStackTrace(cause, pw);
            }
        } else {
            Throwable cause = ex.getCause();

            if (null != cause) {
                pw.println("Cause:");
                fillStackTrace(cause, pw);
            }
        }
    }
}

并在文件facesconfig

中完成此bean定义时已附加
<managed-bean>
            <description>
                Bean que sirve para llenar la especificacion de un error. Stacktrace 
            </description>
            <managed-bean-name>errorDisplay</managed-bean-name>
            <managed-bean-class>mx.com.tdc.datos.page.bean.ErrorDisplay</managed-bean-class>
            <managed-bean-scope>session</managed-bean-scope>
        </managed-bean>

我希望这会发挥作用

答案 3 :(得分:3)

在JSF中向用户显示错误消息的一般方法是使用FacesMessage:

在Java方面:

...
if (errorOccurred) {
    FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("An error occurred blabla..."));
}

在JSF(JSP或XHTML)页面中,只需使用页面中的<h:messages/>组件即可显示错误。

答案 4 :(得分:1)

你可以把

<%@ page errorPage="error.jsp" %>
在jour jsp / jsf页面中。在error.jsp中,jou会有:

<%@ page isErrorPage="true" %>

isErrorPage =“true”将为您的页面提供另一个隐式对象:exception(与您在jsp页面上已有请求和响应的方式相同)。然后,您可以从异常中提取消息。

Additional details

答案 5 :(得分:1)

我遇到同样的问题并找到了描述如何在JSF2中使用ExceptionHandler的链接:

http://jugojava.blogspot.com/2010/09/jsf-2-exception-handling.html