3
votes

I am using JSF,with @ManagedBean annotation and using Primefaces, and I got the following problem.

Why the "onerror" is not triggered? The exception just appears in my console.

login.xhtml

<p:commandButton 
     value="teste" 
     action="#{loginBean.methodTest()}" 
     ajax="true" 
     immediate="false" 
     onerror="confirmation.show()" />

<p:dialog  
    appendToBody="true" 
    header="Atencao" 
    widgetVar="confirmation"  
    showEffect="bounce">  
     ...  
</p:dialog>  

MyBean

@ManagedBean(name = "loginBean") 
@SessionScoped  
public class LoginBean {

    public void methodTest() throws Exception {
        System.out.println("hi");       
        throw new Exception("Exception Test");           
    }
3
could it be that you are using parenthesis () in your action method? depending on what container you are using, that won't work...Elias Dorneles

3 Answers

7
votes

This is the expected behavior...

onerror : Client side callback to execute when ajax request fails.

in your case there is no failure in the ajax request at all , that fact that you are throwing exception got nothing to do with ajax failure

onerror is called when jsf doesn't catch your exception, http error or so. Exception != error.

read this thread for further details Ajax Engine: onerror doesn't work (might provide you with some tips...)

See the following detailed explanation about f:ajax onerror

4
votes

the onerror attribute looks for an http error status code which you need to provide yourself.

In your first example

public void methodTest() {
  ExternalContext context = FacesContext.getCurrentInstance().getExternalContext();
  HttpServletResponse response = (HttpServletResponse) context.getResponse();
  response.setStatus(500); 

  System.out.println("hi");
}

should work.

0
votes

Thanks everybody,

I solved this problem with a not very good way (but it worked out !), but i think perhaps you can help me to improve this way. I would like to throw a Ajax error into my "catch", and the "onerror" (insted of oncomplete) recive it, and open the dialog.

Is there possibility ??

Example that worked out , with a poor way:

    <p:panel id="PanelLogin" header="Login"  >    
      <h:form id="FormLogin"  >
        <br/>

          <h:panelGrid columns="2">
               <h:outputLabel for="user" value="Usuario:" />
               <p:inputText id="user" required="true"   value=" "  size="75"  />    

               <h:outputLabel for="pin" value="Senha:" />
               <p:password id="pin" required="true"  value=" " size="55" />                     
          </h:panelGrid>                                                

         <p:commandButton  styleClass="botaoLogin" value="OK" action="#{loginBean.checkLogin()}" ajax="true" oncomplete="if (#{loginBean.dialog}) confirmation.show()"  />

        </h:form>
</p:panel>

<p:dialog id="atencaoDialog" resizable="false" appendToBody="true" header="Atencao" widgetVar="confirmation"  height="85" width="300" showEffect="bounce">


 <div align="center">                   
   <p:messages id="outputTextAtencaoDialog"  autoUpdate="true" redisplay="false"   />  
 </div>                 


 <div style="text-align: center;">
  <p:commandButton id="okButtonAtencaoDialog" value="OK"  onclick="confirmation.hide();" />                         
 </div>
</p:dialog>

MyBean

 @ManagedBean(name = "loginBean")
    @SessionScoped
    public class LoginBean implements Serializable {

    private boolean dialog;

    ...

    public String checarAutenticacao() {

    ...

    try {

    ...

    return "/templantes/telaAplicacao.xhtml";

     } catch (Throwable e) {

       this.dialog = true;
       // try to throw Ajax error instead of this below ??            
       FacesContext.getCurrentInstance().addMessage(null,new FacesMessage(e.getMessage()));
       return null;
     }
    }