我已经设置了web.xml,以便任何java.lang.Throwable(即任何未捕获的异常或错误)将转发到错误页面.但是,对于AJAXified组件,异常不会通过此机制路由到错误页面.
我所拥有的测试用例是一个简单CommandButton的操作方法,它总是抛出一个RuntimeException.看起来最好的做法是让action方法捕获异常并添加FacesMessage类型错误严重性.这是人们做的吗?有没有办法配置JSF,以便如果AJAXified组件的支持bean方法抛出异常,可以显示错误页面?
简化,我的错误处理程序如下所示:
@Override
public void handle() throws FacesException {
Iterator<ExceptionQueuedEvent> unhandledExceptionQueuedEvents = getUnhandledExceptionQueuedEvents().iterator();
FacesContext context = FacesContext.getCurrentInstance();
if (unhandledExceptionQueuedEvents.hasNext()) {
Throwable exception = unhandledExceptionQueuedEvents.next().getContext().getException();
unhandledExceptionQueuedEvents.remove();
context.getExternalContext().dispatch("/error.jsf");
}
while (unhandledExceptionQueuedEvents.hasNext()) {
unhandledExceptionQueuedEvents.next();
unhandledExceptionQueuedEvents.remove();
}
}
Run Code Online (Sandbox Code Playgroud)
简化的template.xhtml
<html>
<f:view>
<h:head>
<!-- some stuff here -->
</h:head>
<h:body>
<div id="menu"><!-- menu content --></div>
<div id="content">
<ui:insert name="body"></ui:insert>
</div>
</h:body>
</f:view>
</html>
Run Code Online (Sandbox Code Playgroud)
error.xhtml
<html>
<ui:composition template="/template.xhtml">
<ui:define name="body">
ERROR
</ui:define>
</ui:composition>
</html>
Run Code Online (Sandbox Code Playgroud)
但是当渲染error.jsf时,使用ui:composition进行模板化是非常错误的.JSF将template.xhtml渲染到它周围,然后再开始渲染模板.结果是一个页面,菜单呈现两次,所有资源再次包含在页面中间.它的开头看起来像这样:
<div id="menu></div>
<div<?xml version="1.0" encoding="UTF-8" ?="">
<!-- Page included once more --> …Run Code Online (Sandbox Code Playgroud)