2011-04-29 75 views
7

我有一个导入功能,它将解析包含文档版本信息的XML文件并将其保存到数据库中。如果用户尝试上传已经存在的版本,我需要显示确认对话框,如“您已经存在的版本是否要覆盖..?”好的,取消。如何显示支持bean的确认对话框(Primefaces)

我正在使用Mozarra 2.0.3,Prime会面2.2 RC2,Glass Fish 3,我正在尝试这种方式。

<h:form id="conDialog"> 
    <p:commandButton value="getConfirmMsg" update="conDialog" action="#{buttonBean.getConfirmMsg()}" 
     oncomplete="confirmation.show()"/> 
    <p:growl id="messages1" globalOnly="true"/> 
    <p:confirmDialog message="Version already exists. Do you want to override it?" 
     rendered="#{buttonBean.showConfirm}" 
     header="Version already exist" severity="alert" widgetVar="confirmation"> 
     <p:commandButton value="OK" update="messages1" oncomplete="confirmation.hide()" 
      action="#{buttonBean.overrideVersion}" /> 
     <p:commandButton value="Cancel" onclick="confirmation.hide()" type="button" /> 
    </p:confirmDialog> 
</h:form> 

BackingBean

@ManagedBean 
@RequestScoped 
public class ButtonBean { 

    boolean showConfirm = false; 

    public boolean isShowConfirm() { 
     return showConfirm; 
    } 

    public void setShowConfirm(boolean showConfirm) { 
     this.showConfirm = showConfirm; 
    } 

    public void overrideVersion() { 
     System.out.println("Version alrady exists...Overriding..."); 
     FacesMessage msg = new FacesMessage("Action is successful"); 
     FacesContext.getCurrentInstance().addMessage(null, msg); 
    } 

    public void getConfirmMsg() { 
     System.out.println("Inside getConfirmMsg()...."); 
     showConfirm = true; 
     System.out.println("showConfirm: " + showConfirm); 
    } 
} 

当我点击 “OK” 的动作不点火。上述代码中是否有错误?

+0

你到目前为止尝试过什么?只需显示一个对话框就可以使用Primefaces'p:dialog'。 – 2011-05-09 11:21:50

+0

嗨马特,我需要从服务器端做到这一点。如果(VersionExists){显示一个确认对话框。} – neni 2011-05-12 09:03:14

回答

3

在服务器上处理期间,无法从客户端得到确认。呼唤你的操作方法例如之前

  1. 获取改写许可:

    你有两个选择用复选框“覆盖文件如果存在?”或

  2. 您必须停止处理,设置一个标志并返回null以重新加载浏览器中的当前页面。 然后,您可以根据标志状态显示p:dialog

+0

嗨马特,请你看看我发布的代码。 – neni 2011-05-12 11:50:07

+0

@Veerendra commandButton应该可以工作。浏览器中是否有任何javascript错误? – 2011-05-12 12:36:58

+0

不是Matt,没有JavaScript错误。 – neni 2011-05-12 12:38:50

2

您正面临典型的Primefaces问题。

当您的页面显示并且buttonBean.showConfirm = false时,此元素不会呈现。这意味着它不会出现在DOM树中。不管你以后做什么,都不能显示或隐藏不存在的元素。

实际上有两种方法可以解决您的问题。

  1. 使用远程命令,以便未呈现的HTML代码将从您的服务器传输。
  2. 使用CSS“display:none”而不是rendered =“false”。
0

我遇到了非常类似的问题。我想出的解决方案是将逻辑分成2个 - 首先,按下按钮时,使用'action'准备验证数据并使用'oncomplete'运行远程命令,显示确认对话框,其中' OK'是真正的行动。