JSF 2:不显示Ajax错误 [英] JSF 2 : Ajax errors not displayed
问题描述
我目前正在尝试Ajax功能,并且对如何显示执行Ajax时出现的错误消息感到好奇? 例如,我有一个primefaces按钮:
Im currently trying out the ajax feature, and im curious about how can i display the error messages occured when i do the ajax stuff ? For example, i have a primefaces button :
<p:commandButton value="Refresh" update="debugPanel messages"
action="#{checkbocLabBean.submit}"/>
在测试该按钮时,似乎什么也没有发生,直到我检查服务器日志,才有例外.原来,我有错字.应该是#{checkboxLabBean.submit}.
When testing that button, nothing seems to happen, until i check the server logs, there's an exception. It turns out that i have a typo. Should be #{checkboxLabBean.submit}.
我现在唯一想到的就是禁用ajax,在我的情况下添加ajax ="false",错误就会出现.
The only thing i can think of right now is to have disable the ajax, adding ajax="false" in my case, and the error will show up.
在使用ajax请求时,还有其他方法可以在开发阶段将错误显示在浏览器中吗?
Is there any other ways to show errors right into the browser in development phase when using ajax requests ?
推荐答案
Ajax请求是异步发生的.默认情况下,它们不会影响整个文档. PrimeFaces的ajax请求期间引发的异常将委派给 <p:ajaxStatus>
.您可以在<facet name="error">
中完成您的任务.
Ajax requests happens asynchronously. They do by default not affect the entire document. Exceptions thrown during PrimeFaces' ajax requests are delegated to <p:ajaxStatus>
. You can do your thing in <facet name="error">
.
<p:ajaxStatus>
<f:facet name="start">Loading...</f:facet>
<f:facet name="success"><h:outputText value="" /></f:facet>
<f:facet name="error">An error has occurred!</f:facet>
</p:ajaxStatus>
如果您想用错误页面替换当前文档(这是一个非常合理的选择),那么很高兴知道PrimeFaces在后台使用了jQuery,并且您可以按照以下方式配置jQuery:>
If you rather want to replace the current document with the error page -which is a very reasonable choice- then it's good to know that PrimeFaces uses jQuery under the covers and that you can configure jQuery as follows to do so:
<script>
jQuery.ajaxSetup({
error: handleXhrError
});
function handleXhrError(xhr) {
document.open();
document.write(xhr.responseText);
document.close();
}
</script>
另请参见:
- 如何保护服务器上的Web应用程序侧错误?
- 服务器端的处理jQuery的ajax请求中的HTTP错误
- How to safeguard webapplication on server-side errors?
- Handling of server-side HTTP errors in jQuery's ajax requests
See also:
这篇关于JSF 2:不显示Ajax错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!