使用JSF / Facelets处理带有后备bean问题的页面

问题描述

|| 按照此解决方案,我制作了一个如下的错误页面: 在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);
        }
    }
}
有了这段代码,当我在应用程序的另一部分中故意引起某种欺骗(ClassCastException)时,它将我重定向到此页面,但是,通过将调试打印日志放入支持Bean类中,我看到它从未实例化,因此文本该区域不会被填充。 为什么从未实例化后备bean?     

解决方法

那个地点
<location>/errore500.xhtml</location>
必须与“ 4”的网址格式匹配。 因此,例如将其映射到
<url-pattern>*.jsf</url-pattern>
那么您需要将位置设置为
<location>/errore500.jsf</location>