繁体   English   中英

带有JSF的PrimeFaces-“ onerror” <p:commandButton> 不起作用

[英]PrimeFaces with JSF - “onerror ” in the <p:commandButton> doesn't work

我正在使用带有@ManagedBean批注的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>  

豆豆

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

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

这是预期的行为...

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

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

当jsf无法捕获您的异常或HTTP错误时,将调用onerror。 异常!=错误。

阅读此线程以获取更多详细信息Ajax Engine:onerror不起作用 (可能会为您提供一些提示...)

请参阅以下有关f:ajax onerror的详细说明

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

在第一个例子中

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

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

应该管用。

谢谢大家,

我用不是很好的方法解决了这个问题(但是解决了!),但是我认为也许您可以帮助我改进这种方法。 我想在我的“ 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>

豆豆

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM