[英]JSF Handle Exception in Ajax events
我遵循了这个解决方案,并且在发生ViewExpiredException时可以完美地工作,但是当我检查(firefox实用程序)该视图错误重新出现时,我看到它已取代了它,但只是在普通视图的body标签内,我的意思是异常的原因。 视图错误在他自己的body标签中声明了一个css类,但是我不知道,为什么不替换整个view错误,而是仅获取view错误的所有内容(在他的body标签之后)并插入其中正常视图的主体标签?
要获得此行为,我有一个登录视图(我在上面提到的普通视图),只需要等到会话到期,然后尝试登录(提交视图的形式),然后触发ExceptionHandler来呈现视图错误。
以下是一些片段:
login.xhtml
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:p="http://primefaces.org/ui">
<h:head>
<f:facet name="first">
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0"/>
<meta name="apple-mobile-web-app-capable" content="yes" />
</f:facet>
<title>PrimeFaces</title>
</h:head>
<h:body styleClass="login-body">
<div class="login-panel ui-fluid">
<div class="ui-g">
<div class="ui-g-12 logo-container">
<p:graphicImage name="images/logo-colored.png" library="theme-layout" />
<h1>Login to Your Account</h1>
<h2>WELCOME</h2>
</div>
<div class="ui-g-12">
<p:inputText placeholder="User" />
</div>
<div class="ui-g-12">
<p:password placeholder="Password" feedback="false"/>
</div>
<div class="ui-g-12 chkbox-container">
<p:selectBooleanCheckbox id="remember-me" />
<p:outputLabel for="remember-me" value="Remember Me"/>
</div>
<div class="ui-g-12 button-container">
<p:commandButton type="submit" value="Log in" icon="fa fa-user" styleClass="orange-btn" action="#{menu.login}" update="frmLoginPromo">
</div>
</div>
</div>
<h:outputStylesheet name="css/layout-#{guestPreferences.layout}.css" library="theme-layout" />
</h:body>
</html>
error.xhtml
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:p="http://primefaces.org/ui">
<h:head>
<f:facet name="first">
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0"/>
<meta name="apple-mobile-web-app-capable" content="yes" />
</f:facet>
<title>PrimeFaces - Error </title>
</h:head>
<h:body styleClass="exception-body">
<div class="exception-panel">
<p:graphicImage name="images/icon-error.png" library="theme-layout" />
<h1>Error Occured</h1>
<p>An error occured, please try again later.</p>
</div>
<h:outputStylesheet name="css/layout-blue.css" library="theme-layout" />
</h:body>
</html>
CustomExceptionHandler.java
@Override
public void handle() throws FacesException{
final Iterator<ExceptionQueuedEvent> lclExceptionQueue = getUnhandledExceptionQueuedEvents().iterator();
final FacesContext lclFacesContext = FacesContext.getCurrentInstance();
final Map<String, Object> requestMap = lclFacesContext.getExternalContext().getSessionMap();
while (lclExceptionQueue.hasNext()){
ExceptionQueuedEvent event = lclExceptionQueue.next();
ExceptionQueuedEventContext context = (ExceptionQueuedEventContext) event.getSource();
Throwable lclThrowable = context.getException();
try{
if (lclThrowable instanceof ViewExpiredException){
lclFacesContext.setViewRoot(lclFacesContext.getApplication().getViewHandler().createView(lclFacesContext, "/error.xhtml"));
lclFacesContext.getPartialViewContext().setRenderAll(true);
lclFacesContext.renderResponse();
}
}finally{
lclExceptionQueue.remove();
}
}
getWrapped().handle();
}
这是呈现错误视图后的样子
检查视图错误请告诉我做错了什么?
由于您已经在使用PrimeFaces,因此我选择不开发自己的异常处理程序。 PrimeFaces已经拥有一个可以处理ajax和非ajax请求的程序 。
对于不使用PrimeFaces的人,我建议使用OmniFaces异常处理
对于PrimeFaces 6.2,文档包含在11.3章中进行配置的信息(与PF 6.1的第btw章相同)
总结( 所有引号均来自PF文档)
配置El解析器和异常处理程序
<application> <el-resolver> org.primefaces.application.exceptionhandler.PrimeExceptionHandlerELResolver </el-resolver> </application> <factory> <exception-handler-factory> org.primefaces.application.exceptionhandler.PrimeExceptionHandlerFactory </exception-handler-factory> </factory>
如果要在web.xml中配置错误页面
<error-page> <exception-type>java.lang.Throwable</exception-type> <location>/ui/error/error.jsf</location> </error-page> <error-page> <exception-type>javax.faces.application.ViewExpiredException</exception-type> <location>/ui/error/viewExpired.jsf</location> </error-page>
然后,您可以在错误页面中使用有关EL中异常的信息。
<h:outputText value="Message:#{pfExceptionHandler.message}" /> <h:outputText value="#{pfExceptionHandler.formattedStackTrace}" escape="false" />
有更多信息,建议您参考该文档。
对于ajax异常,您可以执行
<p:ajaxExceptionHandler type="javax.faces.application.ViewExpiredException" update="exceptionDialog" onexception="PF('exceptionDialog').show();" /> <p:dialog id="exceptionDialog" header="Exception: #{pfExceptionHandler.type} occured!" widgetVar="exceptionDialog" height="500px"> Message: #{pfExceptionHandler.message} <br/> StackTrace: <h:outputText value="#{pfExceptionHandler.formattedStackTrace}" escape="false" /> <p:button onclick="document.location.href = document.location.href;" value="Reload!"/> </p:dialog>
OmniFaces的配置非常相似。
也可以看看:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.