2011-05-25 74 views
1

this solution我犯了一个错误页面是这样的:错误处理页面/ Facelets的

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)时,它将我重定向到这个页面,但是通过将一个调试打印日志放在backing bean类中,我发现它从未实例化过,因此文本区域不会被填充。 为什么后台bean从未实例化?

回答

0

位置

<location>/errore500.xhtml</location> 

必须FacesServlet的URL模式相匹配。

因此,如果它是例如在

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

映射则需要的位置设置为

<location>/errore500.jsf</location> 
+0

我已经在web.xml这片配置: \t'<上下文-param> ​​javax.faces.DEFAULT_SUFFIX .xhtml ... 面临的Servlet javax.faces.webapp.FacesServlet <负载上启动> 1 面临的Servlet * .faces ' 我应该用errore500.faces更改位置吗? – JLPicard 2011-05-26 08:23:15

+1

是的,FacesServlet的URL模式。 – BalusC 2011-05-26 10:51:01

+0

对不起,但没有奏效。可能是别的东西?也许面临配置的东西? – JLPicard 2011-05-27 11:37:04