2012-07-26 69 views
1

我正在使用JSF,使用ManagedBean註釋並使用Primefaces,並且出現以下問題。使用JSF PrimeFaces - 「onerror」在<p:commandButton>不起作用

我想知道爲什麼「onerror」沒有觸發!該例外只出現在我的控制檯中。

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"> 

...

爲myBean

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

    public void methodTest() throws Exception { 
       System.out.println("hi");  
       throw new Exception("Exception Test");   } 

謝謝大家!

+0

是否可以在操作方法中使用括號'()'?取決於你使用的是什麼容器,這是行不通的...... – elias 2012-07-26 01:41:07

回答

4

這是預期的行爲...

的onerror:客戶端回調時AJAX請求失敗執行。

你的情況

存在Ajax請求沒有失敗可言,這一事實,你扔異常什麼都沒有做與阿賈克斯失敗

的onerror被調用時JSF沒有趕上你的例外,http錯誤等等。例外!=錯誤。

閱讀此線程的詳細信息Ajax Engine: onerror doesn't work(可能爲你提供一些提示...)

請參見下面的詳細說明有關f:ajax onerror

+0

所以當驗證失敗時不會調用onerror,但是當ajax失敗時? – Ced 2015-09-13 21:29:13

+0

@Ced,onerror與jsf驗證無關,請參閱此處更進一步的解釋:http://docs.oracle.com/javaee/6/tutorial/doc/gkdcb.html – Daniel 2015-09-13 22:02:01

0

謝謝大家,

我解決了這個問題與不是非常好的方式(但它解決了!),但我想也許你可以幫助我改進這種方式。我想向我的「catch」中拋出一個Ajax錯誤,並且「onerror」(oncomplete insted)將它收回,並打開對話框。

是否有可能?

的例子,制定出來的,有一個好辦法:

<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; 
    } 
    } 
2

使用onerror屬性查找您需要提供自己的HTTP錯誤狀態代碼。

在你的第一個例子

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

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

應該工作。