Server-side message / exception binding using an AJAX request

I am doing an ajax submit using Prime, but I am unable to associate my server side message with my ajax request. Suppose I have this button that triggers an action. In my managed bean do I need to throw an exception? How to pass this message to 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 work with this?

Also, in one case, the alleged DB does not work, how do I handle the exception? Access 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){

    }
}
+2
source share
1 answer

, ExceptionHandler , , ajax-:

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

, web.xml. web.xml, . OmniFaces , FullAjaxExceptionHandler. .

, , . . FacesMessage ajax <h:messages>, <p:messages> <p:growl>. PrimeFaces autoUpdate="true", ajax. .

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

<p:messages autoUpdate="true" />

, , . , . JPA PersistenceException, , , JSF bean, .

+4

All Articles