【问题标题】:Unable to update component using primefaces update attribute无法使用 primefaces 更新属性更新组件
【发布时间】:2015-04-12 22:16:18
【问题描述】:

我是 JSF 和 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","sravanyahoo@gmail.com");
        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));
    }

【问题讨论】:

  • 你的dataGridView.images值是否被fileUploadView.handleFileUpload方法更新了?
  • 只要更新=":content"
  • 是的,handleFileUpload 方法更新“图像”列表
  • 刚刚更新:内容不起作用

标签: jsf-2 primefaces


【解决方案1】:

您的 dataGrid 很可能在上传和更新之前未呈现 (rendered="#{not empty dataGridView.images}"。如果确实不是,则您遇到jsf bascis 之一,如果组件未呈现,您将无法更新它。

【讨论】:

  • 我已经删除了渲染并尝试过,但没有成功
  • 然后看看上面@AlexandreLavoie 的评论。你用的是什么版本? bean的范围是什么?也许尝试使用 ajax eventHandler。不应该有所作为,但你永远不知道
猜你喜欢
  • 1970-01-01
  • 2011-06-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-20
  • 2016-09-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多