0

我遵循了在 jsf web app 中实现异常处理的说明。

我的问题是显示属性值,我在 ExceptionHandler 中设置

这是 ExceptionHandler.java

    @Override
public void handle() throws FacesException {
    final Iterator<ExceptionQueuedEvent> i = getUnhandledExceptionQueuedEvents().iterator();
    while (i.hasNext()) {

        ExceptionQueuedEvent event = i.next();
        ExceptionQueuedEventContext context = (ExceptionQueuedEventContext) event.getSource();
        Throwable t = context.getException();

        FacesContext fc = FacesContext.getCurrentInstance();
        Map<String, Object> requestMap = fc.getExternalContext().getRequestMap();
        NavigationHandler nav = fc.getApplication().getNavigationHandler();

            try {
                // Push some useful stuff to the request scope for
                // use in the page
                System.err.println("DefaultExceptionHandler.handle()...exceptionMessage = " + t.getMessage());
                requestMap.put("exceptionMessage", t.getMessage());
                nav.handleNavigation(fc, null, "error/500");
                fc.renderResponse();
            } finally {
                i.remove();
            }
        }
    getWrapped().handle();
}

和 500.xhtml

<ui:composition  
 xmlns="http://www.w3.org/1999/xhtml"  
 xmlns:h="http://java.sun.com/jsf/html"  
 xmlns:ui="http://java.sun.com/jsf/facelets"  
 xmlns:f="http://java.sun.com/jsf/core">  


   The error message is:  
   <br />
   <b>  
       3. :  #{exceptionMessage}  
   </b>  
   <br />
   <b>  
       1. :  #{requestScope['exceptionMessage']}  
   </b>  
   <br />
   <b>  
       2. :  #{param['exceptionMessage']}  
   </b>  

浏览器中的页面如下所示:

    The error message is: 
3. : 
1. : 
2. :

提前致谢!!

4

1 回答 1

0

首先,这根本不是请求参数。请求参数是最终用户随 HTTP 请求一起发送到服务器的数据。在 GET 请求中,它们在 URL 的查询字符串部分中可见。在 POST 请求中,它们隐藏在请求正文中(但通过 HTTP 流量监视器可见,例如浏览器的内置监视器,可通过 F12 键访问)。您设置的是请求属性。因此,从尝试来看,只有#{exceptionMessage}并且#{requestScope['exceptionMessage']}应该有效,但#{param['exceptionMessage']}绝对不是。

回到您的具体问题,当您通过导航案例发送导航重定向时,或者通过在自定义导航处理程序中的某处<redirect>附加结果时,可能会发生这种情况。?faces-redirect=true重定向基本上指示客户端创建一个全新的 HTTP 请求,从而丢弃包含其所有属性的初始 HTTP 请求。

如果这也不是问题,那么正在使用的(定制的)FacesContextExternalContext实现可能会损坏。很难说,因为您没有告诉任何关于所使用的 JSF impl/version、所使用的服务器或任何“第三方”库的信息。例如,众所周知,Spring Web Flow 的ExternalContext实现在某些版本中会被破坏。

于 2013-06-18T15:47:12.130 回答