我正在为特定的异常类型设计一个错误页面。
<error-page>
<exception-type>xxx.AbstractConfigurableNotFoundException</exception-type>
<location>/xxx/page-not-found.xhtml</location>
</error-page>
我正在使用 OmniFaces 及其异常工厂来处理错误。
<factory>
<exception-handler-factory>org.omnifaces.exceptionhandler.FullAjaxExceptionHandler</exception-handler-factory>
</factory>
AbstractConfigurable
是一个域类,它的实例是在模板评估期间创建的*。 AbstractConfigurable
的构造函数可能会抛出 AbstractConfigurableNotFoundException
.当它发生时,这个异常作为一个原因被包装到特定于模板的异常中。javax.servlet.ServletException
Caused by: javax.faces.view.facelets.TagAttributeException
Caused by: com.sun.faces.mgbean.ManagedBeanCreationException
...
Caused by: xxx.AbstractConfigurableNotFoundException
结果,出现了不同的异常类型和我的
<error-page>
逻辑不适用。明明不想管,
javax.faces.view.facelets.TagAttributeException
<error-page>
<exception-type>javax.faces.view.facelets.TagAttributeException</exception-type>
<location>/xxx/page-not-found.xhtml</location>
</error-page>
但我想用
AbstractConfigurableNotFoundException
处理任何异常作为一个原因。有什么我可以做的吗?
最佳答案
FullAjaxExceptionHandler
支持上下文参数来设置要解包的异常类型的完全限定名称。以下是其 documentation 的相关摘录:
Configuration
By default only
FacesException
andELException
are unwrapped. You can supply a context parameter "org.omnifaces.EXCEPTION_TYPES_TO_UNWRAP" to specify additional exception types to unwrap. The context parameter value must be a commaseparated string of fully qualified names of additional exception types. Note that this also covers subclasses of specified exception types.<context-param> <param-name>org.omnifaces.EXCEPTION_TYPES_TO_UNWRAP</param-name> <param-value>javax.ejb.EJBException,javax.persistence.RollbackException</param-value> </context-param>
This context parameter will also be read and used by
FacesExceptionFilter
.
因此,在您的特定情况下,您可以在
web.xml
中使用以下配置:<context-param>
<param-name>org.omnifaces.EXCEPTION_TYPES_TO_UNWRAP</param-name>
<param-value>com.sun.faces.mgbean.ManagedBeanCreationException</param-value>
</context-param>
请注意
javax.faces.view.facelets.TagAttributeException
延伸自 FacesException
,默认情况下已经解包,所以你真的不需要指定它。
关于jsf - 处理模板评估期间发生的域异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57143407/