0

我想使用 Primefaces 文件上传,这是我的 jsf 页面:

 <h:form id="uploadForm" enctype="multipart/form">  

    <p:fileUpload id="fileUpload" value="#{fileUploadController.file}" mode="simple"/>  

    <p:commandButton value="Submit" ajax="false"  
                                 actionListener="#{fileUploadController.upload}" />               
 </h:form> 

这是我的豆子:

package FileUploadController;


import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import org.primefaces.model.UploadedFile;

@ManagedBean
@SessionScoped
public class FileUploadController {  

    private UploadedFile file; 

    public UploadedFile getFile() {  
        return file;  
    }  

    public void setFile(UploadedFile file) {  
        this.file = file;  
    }  

    public void upload() {  
        if(file != null) {  
            FacesMessage msg = new FacesMessage("Succesful", file.getFileName() + " is uploaded.");  
            FacesContext.getCurrentInstance().addMessage(null, msg);  
        }  
    }  
}

这是 prim 所需的依赖项。文件上传,pom.xml(我使用 maven 项目):

<dependency>
    <groupId>commons-fileupload</groupId>
    <artifactId>commons-fileupload</artifactId>
    <version>1.2.1</version>
</dependency>     
<dependency>
     <groupId>org.apache.commons</groupId>
     <artifactId>commons-io</artifactId>
     <version>1.3.2</version>
</dependency>
<dependency>
     <groupId>portlet-api</groupId>
     <artifactId>portlet-api</artifactId>
     <version>1.0</version>
</dependency> 

这是我的 web.xml:

    <filter>
        <filter-name>PrimeFaces FileUpload Filter</filter-name>
        <filter-class>
            org.primefaces.webapp.filter.FileUploadFilter
        </filter-class>
    </filter>
    <filter-mapping>
        <filter-name>PrimeFaces FileUpload Filter</filter-name>
        <servlet-name>Faces Servlet</servlet-name>
    </filter-mapping>

我想设置UploadedFile文件。当我单击提交按钮时,上传方法被正确调用,但UploadedFile文件仍然为空,未设置为所选文件。我怎样才能设置这个文件?

4

1 回答 1

1

enctype属性enctype="multipart/form"无效。该enctype属性指定用于将表单提交到服务器的内容类型。

根据 PrimeFaces 文档(第 183 页),该enctype属性应定义为enctype="multipart/form-data",在您的情况下:

<h:form id="uploadForm" enctype="multipart/form-data">
    ...
</h:form>
于 2012-11-14T22:40:16.107 回答