使用JSF的PrimeFaces-“;onerror";在<;p:commandButton>;不';行不通

使用JSF的PrimeFaces-“;onerror";在<;p:commandButton>;不';行不通,jsf,primefaces,exception-handling,dialog,onerror,Jsf,Primefaces,Exception Handling,Dialog,Onerror,我正在使用JSF、注释和Primefaces,我遇到了以下问题 为什么未触发“onerror”?异常只出现在我的控制台中 login.xhtml <p:commandButton value="teste" action="#{loginBean.methodTest()}" ajax="true" immediate="false" onerror="confirmation.show()" /> <p:dialo

我正在使用JSF、注释和Primefaces,我遇到了以下问题

为什么未触发“
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">  
     ...  
</p:dialog>  

这是预期的行为

OneError:当ajax请求失败时执行的客户端回调

在您的情况下,ajax请求中根本没有失败,您抛出异常的事实与ajax失败无关

当jsf没有捕捉到您的异常、http错误等时,就会调用onerror。例外!=错误

阅读此线程以了解更多详细信息(可能会为您提供一些提示…)

请参见以下关于

的详细说明,谢谢大家

我用一个不太好的方法解决了这个问题(但它成功了!),但我想也许你可以帮助我改进这个方法。我想在我的“catch”中抛出一个Ajax错误,“onerror”(而不是oncomplete)接收它,并打开对话框

有可能吗

用一种不好的方法得到了结果的例子:

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

onerror属性查找您需要自己提供的http错误状态代码

在您的第一个示例中

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

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

应该有效。

是否在操作方法中使用了括号
()
?根据您使用的容器的不同,这将不起作用……因此当验证失败时不会调用oneror,但当ajax失败时会调用oneror?@Ced,oneror与jsf验证无关,请参阅此处的oneror进一步解释:
 @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;
     }
    }
public void methodTest() {
  ExternalContext context = FacesContext.getCurrentInstance().getExternalContext();
  HttpServletResponse response = (HttpServletResponse) context.getResponse();
  response.setStatus(500); 

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