2015-02-12 111 views
0

我是JSF和primefaces的新手。我想在文件上傳完成時更新數據網格。我累更新屬性,但它沒有更新組件。我只能在重新加載頁面後才能看到更新的數據網格。無法使用primefaces更新屬性更新組件

<h:form> 

    <p:fileUpload fileUploadListener="#{fileUploadView.handleFileUpload}" mode="advanced" dragDropSupport="false" 
        multiple="true" update=":content:images" allowTypes="/(\.|\/)(gif|jpe?g|png)$/" /> 

    <p:growl id="messages" showDetail="true" /> 
</h:form> 

     <h:form id="content"> 
     <p:dataGrid var="image" value="#{dataGridView.images}" columns="2" 
        rows="12" paginator="true" id="images" rendered="#{not empty dataGridView.images}" 
     paginatorTemplate="{CurrentPageReport} {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}" 
     rowsPerPageTemplate="6,12,16" > 

     <f:facet name="header"> 
      Your Album 
     </f:facet> 

     <p:panel header="#{image.imageName}" style="text-align:center;"> 
      <h:panelGrid columns="1" style="width:100%"> 
       <image src="#{image.imageLocation}" style="height: 10%;width:30%;"/> 
      </h:panelGrid> 

     </p:panel> 

    </p:dataGrid> 

</h:form> 

handleFileUpload方法

public void handleFileUpload(FileUploadEvent event) { 
     currentSession.setAttribute("username","[email protected]"); 
     String userName = (String) currentSession.getAttribute("username"); 
     String realPath = ctx.getRealPath("/"); 
     String contextPath = ctx.getContextPath();  
     try{ 
     UploadedFile uploadedFile = event.getFile(); 
     String imageLocation = ImageUtils.uploadFile(realPath,contextPath,uploadedFile,userName); 
     } 
     catch(Exception e) 
     { 

     } 
     helper.updateImageList(userName,dataGridView); 
     FacesMessage message = new FacesMessage("Succesful", event.getFile().getFileName() + " is uploaded."); 
     FacesContext.getCurrentInstance().addMessage(null, message); 

    } 

updateImageList方法

public void updateImageList(String userName,DataGridView view){  
     view.setImages(getImagesFromDB(userName)); 
    } 
+0

您的'dataGridView.images'值是否由'fileUploadView.handleFileUpload'方法更新? – 2015-02-12 09:01:49

+0

只是做更新=「:內容」 – 2015-02-12 10:18:17

+0

是的,handleFileUpload方法更新「圖像」列表 – user3173392 2015-02-12 10:43:56

回答

0

最有可能你的數據網格沒有上傳和更新(rendered="#{not empty dataGridView.images}"之前渲染。如果確實不行,你正在運行到jsf bascis之一,如果它未呈現,則無法更新組件。

+0

我已經刪除渲染和嘗試,但它沒有奏效 – user3173392 2015-02-12 10:44:49

+0

然後看看上面的評論@AlexandreLavoie。你用什麼版本?這個bean的範圍是什麼?也許嘗試使用ajax eventHandler。不應該有所作爲,但你永遠不會知道 – Kukeltje 2015-02-12 10:52:48