链接服务器端消息/异常AJAX请求 [英] Linking Server Side Message/Exception with AJAX request

查看:248
本文介绍了链接服务器端消息/异常AJAX请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想提出一个ajax提交使用Primefaces,但我有我的连接服务器端的消息,我的Ajax请求的麻烦。 本来我有这个按钮调用的动作。 在我管理的bean,我需要提出一个例外?如何传递这个信息到我的Ajax请求

 公共无效结帐(ActionEvent的事件){
    如果(如pression){
        抛出新CustomException(账户余额是不够的!);
    }
}

公共类CustomException扩展RuntimeException的{
    公共CustomException(字符串消息){
        超(消息);
    }
}
 

我要如何处理这种情况?请问我的onError JavaScript方法能处理呢?

此外,在一种情况下应该DB是先降后我该如何​​处理异常?难道我已经进入到了错误信息 在我的javascript函数?

 公共无效结帐(ActionEvent的事件){
    尝试{
        // DB下跌
        如果(如pression){
            抛出新CustomException(账户余额是不够的!);
        }
    }赶上(例外五){

    }
}
 

解决方案

关于你的具体问题,你需要实现一个自定义的 的ExceptionHandler 这其中确实的基本的下面时在Ajax请求时发生异常:

 字符串errorPageLocation =/WEB-INF/errorpages/500.xhtml;
context.setViewRoot(context.getApplication()getViewHandler()CreateView的(上下文,errorPageLocation));
context.getPartialViewContext()setRenderAll(真)。
context.renderResponse();
 

如果你想利用的web.xml 的错误页面,考虑到这是不完全微不足道。你需要解析整个的web.xml 此找到错误页面的位置。该 OmniFaces 工具库正好有这样一个异常处理函数,<一个href="http://omnifaces.org/docs/javadoc/current/org/omnifaces/exceptionhandler/FullAjaxExceptionHandler.html"相对=nofollow> FullAjaxExceptionHandler 。你可以找到完整的源$ C ​​$ C <一个href="https://github.com/omnifaces/omnifaces/blob/master/src/org/omnifaces/exceptionhandler/FullAjaxExceptionHandler.java"相对=nofollow>此处和展示的例子这里

至于你的具体功能要求,我就不会在这里只有一个用户错误抛出异常。这完全恢复。您需要创建并添加的FacesMessage 并具有AJAX更新&LT; H:邮件&gt; &LT;电话号码:邮件&gt; &LT;电话号码:咆哮&GT; 。该PrimeFaces那些支持自动更新=真正的这将自动更新本身Ajax请求。例如,

  context.addMessage(空,新的FacesMessage(FacesMessage.SEVERITY_ERROR,余额不足,空));
 

&LT;电话号码:信息自动更新=真/&GT;

抛出异常使得像为DB时,为降不可恢复的情况下,只可意会。需要注意的是,你通常不会抛出这样的异常的自己的。如果JPA的,将已经抛出的PersistenceException ,你又不应JSF搭上托管bean,而只是让他走。

I am making an ajax submit using Primefaces but I am having trouble linking my server side message with my ajax request. Supposed I have this button that calls an action. In my managed bean, do I need to raise an exception? How do I pass this message into my ajax request

public void checkout(ActionEvent event){
    if(expression){
        throw new CustomException("Account balance is not enough!");
    }
}

public class CustomException extends RuntimeException { 
    public CustomException(String message) {
        super(message);
    }
}

How do I handle this case? Will my onerror javascript method be able to handle this?

Also, in one case supposed DB is down then how do I handle the exception? Do I have accessed to the error message in my javascript function?

public void checkout(ActionEvent event){
    try{
        //DB is down
        if(expression){
            throw new CustomException("Account balance is not enough!");
        }
    }catch(Exception e){

    }
}

解决方案

As to your concrete question, you need to implement a custom ExceptionHandler for this which does basically the following when an exception occurs in an ajax request:

String errorPageLocation = "/WEB-INF/errorpages/500.xhtml";
context.setViewRoot(context.getApplication().getViewHandler().createView(context, errorPageLocation));
context.getPartialViewContext().setRenderAll(true);
context.renderResponse();

This is not exactly trivial if you want to take web.xml error pages into account. You'd need to parse the entire web.xml for this to find the error page locations. The OmniFaces utility library has exactly such an exception handler, the FullAjaxExceptionHandler. You can find the full source code here and the showcase example here.

As to your concrete functional requirement, I wouldn't throw an exception when there's just an user error. This is fully recoverable. You need to create and add a FacesMessage and have ajax to update the <h:messages>, <p:messages> or <p:growl>. The PrimeFaces ones support an autoUpdate="true" which would auto-update itself on ajax requests. E.g.

context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Insufficient balance", null));

with

<p:messages autoUpdate="true" />

Throwing an exception makes only sense in unrecoverable situations like as when the DB is down. Note that you usually don't throw such an exception yourself. In case of JPA it would already be thrown as PersistenceException which you in turn shouldn't catch in JSF managed bean, but just let it go.

这篇关于链接服务器端消息/异常AJAX请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆