1

我有 ErrorPage.jsp 我有

<h:messages styleClass="messageError" id="messages1"></h:messages>

当支持 bean 构造函数中发生异常时,我捕获它并执行以下操作

public constructorxxx throws Exception{
    // code 
// code 
// code
catch(Exception e){
try{
        LOG.error(e);
        String customMessage = "An Unknown Error At " + e.getStackTrace()[0].toString() +  "at" + message;

        getFacesContext().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, 
                customMessage, null));
throw new Exception();
                }catch (IOException exception){   
            LOG.error(exception);   
    } 
}
} // end of constructor

在我的 Web.xml 中,我使用了以下标签。

<error-page>
<exception-type>java.lang.Exception</exception-type>
<location>/ErrorPage.jsp</location>    

当我这样做时,我收到以下错误

1) Uncaught service() exception root cause Faces Servlet: javax.servlet.ServletException
2) An exception was thrown by one of the service methods of the servlet [/sc00/ErrorPage.jsp] in application [MembershipEligibilityScreensEAR]. Exception created : [java.lang.RuntimeException: FacesContext not found.

and in my page it displays as
SRVE0260E: The server cannot use the error page specified for your application to handle the Original Exception printed below.

Error Message: javax.servlet.ServletException
Error Code: 500
Target Servlet: Faces Servlet
Error Stack: 
java.lang.Exception 
// stack trace


Error Message: java.lang.RuntimeException: FacesContext not found
Error Code: 0
Target Servlet: 
Error Stack: 
java.lang.RuntimeException: FacesContext not found 

很多人要求我将 ErrorPage.jsp 的位置更改为 /sc00/ErrorPage.faces 但它在我的 web.xml 上显示链接断开警告,错误是网页无法显示和编程错误。

我正在使用 jsf 1.2,而我的“ErrorPage.jsp”没有支持 bean。

谁能建议我为什么没有显示 Error.jsp ?

4

1 回答 1

3

Faces 消息是请求范围的,因此它们具有与当前 HTTP 请求-响应周期相同的生命周期。但是,您是在指示网络浏览器通过发送重定向来创建新的 HTTP 请求。新的 HTTP 请求中不再存在面孔消息。

您最好只抛出一个异常,并通过<error-page>web.xml. servletcontainer 将自动转发到同一请求中的特定错误页面。

例如

getFacesContext().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, customMessage, null));
throw new SomeException();

<error-page>
    <exception-type>com.example.SomeException</exception-type>
    <location>/sc00/ErrorPage.faces</location>
</error-page>

但是鉴于您的特定容器和 JSF impl/version 显然无法转发到基于 JSF 的错误页面(它是否在无限循环中运行?),那么您最好的选择是从错误页面中删除所有 JSF 组件(否则您将得到RuntimeException: FacesContext not found) 并使它成为一个只有 HTML 和 JSTL 的真正普通的 JSP 页面。

<error-page>
    <exception-type>com.example.SomeException</exception-type>
    <location>/sc00/ErrorPage.jsp</location>
</error-page>

您应该只将消息放在异常本身中,而不是添加为面孔消息。

throw new SomeException(customMessage);

然后,您可以在错误页面中显示如下:

${requestScope['javax.servlet.error.message']}

您甚至可以让异常消失(即只需将其重新声明为throws操作方法。

public void doSomething() throws SomeException {
    // ...
}

无论如何,servletcontainer 都会自动记录它。

于 2013-01-08T00:20:13.560 回答