简体   繁体   English

JSF 2.0 Ajax异常处理

[英]JSF 2.0 Ajax Exception Handling

I have written the following code in our xhtml file , for redirecting to error page for ajax calls, I have written it in common page. 我在我们的xhtml文件中编写了以下代码,用于重定向到ajax调用的错误页面,我已经将它写在了公共页面中。

var onError = function onError() {
window.location = 'http://' + jQuery(location).attr('host')+'/saec/app/error';//saec/app/error };jsf.ajax.addOnError(onError);

It is working fine fine , but in browser am getting the error as "Jsf undefined". 它工作正常,但在浏览器中得到的错误为“Jsf undefined”。

I have tried it by including the 我试过通过包括

h:outputScript name="jsf.js" library="javax.faces" target="head" 

also but its giving the same error. 但它也给出了同样的错误。

Can any one Please tell me where I did the mistake 可以任何人请告诉我我在哪里犯了错误

but in browser am getting the error as "Jsf undefined". 但在浏览器中我得到错误为“Jsf undefined”。

Are you sure that this isn't a typo from your side? 你确定这不是你身边的错字吗? It's jsf in all lowercase, not Jsf . 它是全部小写的jsf ,而不是Jsf

In any case, you will get this error whenever you try to reference the jsf object before it is ever been declared in JavaScript context. 在任何情况下,只要您尝试在JavaScript上下文中声明jsf对象之前引用它,就会收到此错误。 For example, when the generated HTML output (as you can see by View Source in webbrowser) look like this: 例如,生成的HTML输出(如webbrowser中的View Source所示 )如下所示:

<script>jsf.ajax.addOnError(functionName);</script>
<script src="/contextname/javax.faces.resource/jsf.js.xhtml?ln=javax.faces"></script>

You should make sure that you reference it after it is been declared: 您应确保在声明引用它:

<script src="/contextname/javax.faces.resource/jsf.js.xhtml?ln=javax.faces"></script>
<script>jsf.ajax.addOnError(functionName);</script>

Also, you need to take into account the fact that JSF will only auto-include it whenever there's a <f:ajax> tag elsewhere in the view. 此外,您需要考虑这样一个事实:只要视图中的其他地方有<f:ajax>标记,JSF就会自动包含它。 You might want to add an extra check if this is the case or not: 如果是这种情况,您可能需要额外检查:

if (typeof jsf !== 'undefined') {
    jsf.ajax.addOnError(functionName);
}

Otherwise, you need to add the following line to make absolutely sure that JSF will auto-include the ajax script on every request even though the view doesn't contain any <f:ajax> . 否则,您需要添加以下行以确保JSF将在每个请求上自动包含ajax脚本,即使该视图不包含任何<f:ajax>

<h:outputScript library="javax.faces" name="jsf.js" target="head" />

Unrelated to the concrete problem, if you'd like to use the standard <error-page> mechanisms in web.xml on ajax requests as well, then consider using this FullAjaxExceptionHandler instead. 具体问题无关 ,如果您还希望在ajax请求中使用web.xml的标准<error-page>机制,那么请考虑使用此FullAjaxExceptionHandler

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM