如何使用Primefaces在jsf中上传文件<p:confirm />

时间:2019-11-13 08:54:42

标签: jsf primefaces

您好,我尝试使用质数上载文件,当我在“提交”按钮中没有确认对话框时,该文件工作正常。但是当我添加托管bean时,甚至没有调用它。

这是我的xhtml代码:

<p:panel header="New Business Registration" style=" min-height: 400px"> 

    <h:form id="upload" enctype="multipart/form-data">
        <p:messages closable="true" />
        <p:panelGrid columns="2">
        <p:outputLabel value="Select Image"/>
           <p:fileUpload mode="simple" value="#{fileUploadMBean.uploadedFile}"/>
          p:outputLabel value=""/>
          <p:commandButton  update="upload" ajax="false" action="#{fileUploadMBean.uploadFile}" icon="ui-icon-person" value="Register New Client">
                <p:confirm header="Confirm" message="Are you sure you wish to upload this file" />
         </p:commandButton>
          </p:panelGrid>
     </h:form>   
    <p:confirmDialog global="true" showEffect="fade" hideEffect="fade">
                <p:commandButton value="Yes" style="color: #fff;
                                 background-color: #5cb85c;
                                 border-color: #4cae4c;" type="button" styleClass="ui-confirmdialog-yes" icon=" ui-icon-check" />
                <p:commandButton value="No" style="background-color: #d9534f;  border-color: #d43f3a;" type="button" styleClass="ui-confirmdialog-no" icon="ui-icon-closethick" />
            </p:confirmDialog>
 </p:panel>

还有我的托管bean:

@ManagedBean
@ViewScoped
public class FileUploadMBean implements Serializable {
      private UploadedFile uploadedFile;
public String uploadFile() throws IOException {
            System.out.println("hello:"); //method not called when i use confirm dialog but when i dont use file upload is successfull
            FacesContext context = FacesContext.getCurrentInstance();
            if(uploadedFile==null)
            {

                        context.addMessnage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Blank File detected", "image"));
                return "";
            }
            //jsf file upload here
            {}//upload code here which works fine without p:confirm

            return null;
            }

当我使用

<p:commandButton update="upload" action="#{fileUploadMBean.uploadFile}" icon="ui-icon-person" value="Register New Client">


      </p:commandButton>

上传的文件为空;

当我禁用Ajax并且不喜欢

<p:commandButton update="upload" action="#{fileUploadMBean.uploadFile}" icon="ui-icon-person" ajax="false" value="Register New Client">


      </p:commandButton>

上传成功。

当我现在在“提交”按钮中禁用ajax时添加时,根本不会调用托管方法

 <p:commandButton update="upload" action="#{fileUploadMBean.uploadFile}" icon="ui-icon-person" ajax="false" value="Register New Client">

          <p:confirm header="Confirm" message="Are you sure you wish to upload this file" />
          </p:commandButton>

0 个答案:

没有答案