【问题标题】:Error handling page with backing bean problem with JSF/Facelets带有 JSF/Facelets 支持 bean 问题的错误处理页面
【发布时间】:2011-09-01 22:12:17
【问题描述】:

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);
        }
    }
}

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

【问题讨论】:

    标签: jsf error-handling facelets


    【解决方案1】:

    位置

    <location>/errore500.xhtml</location>
    

    必须与 FacesServlet 的 URL 模式匹配。

    所以如果它例如被映射到

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

    那么你需要将位置设置为

    <location>/errore500.jsf</location>
    

    【讨论】:

    • 我在 web.xml 中有这样的配置:&lt;context-param&gt; &lt;param-name&gt;javax.faces.DEFAULT_SUFFIX&lt;/param-name&gt; &lt;param-value&gt;.xhtml&lt;/param-value&gt; &lt;/context-param&gt; ... &lt;servlet&gt; &lt;servlet-name&gt;Faces Servlet&lt;/servlet-name&gt; &lt;servlet-class&gt;javax.faces.webapp.FacesServlet&lt;/servlet-class&gt; &lt;load-on-startup&gt;1&lt;/load-on-startup&gt; &lt;/servlet&gt; &lt;servlet-mapping&gt; &lt;servlet-name&gt;Faces Servlet&lt;/servlet-name&gt; &lt;url-pattern&gt;*.faces&lt;/url-pattern&gt; &lt;/servlet-mapping&gt; 我应该用errore500.faces 改变位置吗?
    • 是的,FacesServlet 的 URL 模式。
    • 抱歉,没用。可能是别的东西?也许面临配置问题?
    猜你喜欢
    • 2012-06-27
    • 2011-02-10
    • 2018-04-04
    • 1970-01-01
    • 1970-01-01
    • 2011-07-23
    • 1970-01-01
    • 2011-04-07
    • 2011-10-13
    相关资源
    最近更新 更多