使用JSF / Facelets的辅助bean问题处理页面时出错

时间:2011-05-25 16:17:02

标签: jsf error-handling facelets

关注this solution我创建了一个错误页面:

在faces-config.xml

<error-page>
    <error-code>500</error-code>
    <location>/errore500.xhtml</location>
</error-page>

....

<managed-bean>
    <managed-bean-name>errore</managed-bean-name>
    <managed-bean-class>it.jlp.prometheus.modello.Errore</managed-bean-class>
    <managed-bean-scope>request</managed-bean-scope>
</managed-bean>

...页面errore500.xhtml ...

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="it"
  xmlns:ui="http://java.sun.com/jsf/facelets"
  xmlns:h="http://java.sun.com/jsf/html">

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
    <meta name="author" content="JLP" />
    <link rel="stylesheet" href="css/stile.css" type="text/css" />
    <link rel="icon" href="icons/favicon.png" type="image/png" />
    <title>Prometheus - Error 500</title>
</head>

<body>
    <h:form>

....

 <br/><h:inputTextarea style="width: 100%;" rows="20" readonly="true"
                                          value="#{errore.stackTrace}" />

 ....

......和Errore班级

public class Errore implements Serializable {

private Log logger = LogFactory.getLog(Errore.class);

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);
    logger.info("****** getStackTrace executed ");
    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);
        }
    }
}

使用这段代码,当我故意在应用程序的另一部分引起一个ecception(一个ClassCastException)时,它会将我重定向到这个页面,但是,通过在支持bean类中放置一个调试打印日志,我看到它从未实例化,因此文本区域不会填充。 为什么从不实例化支持bean?

1 个答案:

答案 0 :(得分:0)

位置

<location>/errore500.xhtml</location>

必须与FacesServlet的网址格式相匹配。

因此,如果它是例如映射在

<url-pattern>*.jsf</url-pattern>

然后你需要将位置设置为

<location>/errore500.jsf</location>