PrimeFaces与JSF -“<p:commandButton>”中的“onerror”无法工作

3
我正在使用JSF,使用@ManagedBean注释,并使用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>  

MyBean

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

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

你是否在你的操作方法中使用了括号 ()?根据你使用的容器,这可能行不通... - Elias Dorneles
3个回答

7

这是预期的行为...

onerror:当ajax请求失败时,客户端回调执行。

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

当jsf未捕获您的异常、http错误等时,才会调用onerror。异常!=错误。

阅读Ajax Engine: onerror doesn't work此主题以获取更多详细信息(可能会为您提供一些提示...)

有关f:ajax onerror的详细说明,请参见以下内容


那么当验证失败时,onerror不会被调用,但当ajax失败时会被调用吗? - Ced
@Ced,onerror与jsf验证无关,请在此处查看进一步的onerror解释:http://docs.oracle.com/javaee/6/tutorial/doc/gkdcb.html - Daniel

4

onerror属性寻找你自己提供的HTTP错误状态码。

在你的第一个例子中:

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

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

应该可以工作。


0

谢谢大家,

我用了一种不太好的方法(但它起作用了!)解决了这个问题,但我认为也许你们可以帮助我改进这种方式。我想在我的“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>

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接