繁体   English   中英

渲染的属性在 Primefaces 3.5 文件上传中无法正常工作

[英]attribute rendered is not working properly with Primefaces 3.5 fileupload

我的目的是在 PrimeFaces v3.5 中fileUpload成功后隐藏上传组件。 以下是部分观点——

<h:form>
<!-- Here are some more components of PrimeFaces, So i am not updating the entire form-->
    <p:fileUpload id="fileUpload"
        rendered="#{!fileUploadController.hidden}"
        label="Choose Script to upload here"
        style="font-size: 100% !important;" showButtons="false"
        fileUploadListener="#{fileUploadController.upload}"
        mode="advanced" auto="true" sizeLimit="100000"
        allowTypes="/(\.|\/)(py|txt)$/" update="fileUpload" />
<!-- Here are some more components of PrimeFaces, So i am not updating the entire     
</h:form>

我的Managed Bean如下-

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Serializable;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import org.apache.commons.io.FilenameUtils;
import org.primefaces.event.FileUploadEvent;
@ManagedBean
@ViewScoped
public class FileUploadController implements Serializable {
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    // private String destination = null;
    private String destination = "D:/temp";
    private boolean hidden = false;
    public FileUploadController() {
//      System.out.println("destination=" + destination);
    }
    // private String destination = "D:/download";
    public void upload(FileUploadEvent event) {
        String fileName = FilenameUtils.getName(event.getFile().getFileName());
        FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_INFO,
                "Success! ", fileName + " is uploaded.");
        FacesContext.getCurrentInstance().addMessage(null, msg);
        try {
            System.out.println("I am trying to copy it...");
            copyFile(fileName, event.getFile().getInputstream());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public void copyFile(String fileName, InputStream in) {
        try {
            System.out.println("filename= " + fileName + ", dest="
                    + destination);
            // write the inputStream to a FileOutputStream
            OutputStream out = new FileOutputStream(new File(destination
                    + File.separator + fileName));
            int read = 0;
            byte[] bytes = new byte[1024];
            while ((read = in.read(bytes)) != -1) {
                out.write(bytes, 0, read);
            }
            in.close();
            out.flush();
            out.close();
            System.out.println("New file created!");
            setHidden(true);
        } catch (IOException e) {
            System.out.println("ERROR " + e.getMessage());
        }
    }
    /**
     * @return the hidden
     */
    public boolean isHidden() {
        return hidden;
    }
    /**
     * @param hidden
     *            the hidden to set
     */
    public void setHidden(boolean hidden) {
        this.hidden = hidden;
    }
}

文件上传成功完成,但fileUpload没有隐藏。

您不能ajax更新有条件地呈现的组件。 JSF ajax引擎JavaScript不会在HTML DOM树中添加/删除更新目标,而是替换HTML DOM树中的更新目标。 如果没有呈现JSF组件,那么JSF ajax引擎将无法替换任何内容。 相反,您需要ajax更新始终呈现的父级。 您可以使用其中的<h:panelGroup>

<h:panelGroup id="fileUploadGroup">
    <p:fileUpload id="fileUpload"
        rendered="#{!fileUploadController.hidden}"
        ...
        update="fileUploadGroup" />
</h:panelGroup>

同样的解决方案也适用于我。 非常感谢!!!

暂无
暂无

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

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