简体   繁体   English

Primefaces 4.0的上载界面不会触发

[英]Upload Interface with Primefaces 4.0 doesn't fire

I'm doing a webapplication running on an Apache Tomcat 7 using Primefaces 4.0. 我正在使用Primefaces 4.0在Apache Tomcat 7上运行Web应用程序。 Trying to realize an upload interface similar to the Primefaces ShowCase, the upload interface doesn't fire any function in the backing bean: 为了实现类似于Primefaces ShowCase的上载接口,上载接口在后备Bean中没有触发任何功能:

Here's what I included directly in my body (I also tried without "multiple", "value", advanced mode, "actionListener" attributes) 这就是我直接包含在我体内的内容(我也尝试了不使用“多个”,“值”,高级模式和“ actionListener”属性)

                            <p:fileUpload id="fileupload_" 
                            value="#{userWizard.fu.uploadedFile}" 
                            fileUploadListener="#{userWizard.fu.addFileToAttachment}" 
                            actionListener="#{userWizard.fu.listener}" 
                            mode="advanced"
                            dragDropSupport="false"
                            update="messages"
                            multiple="false"
                            sizeLimit="50000000"
                            fileLimit="3" 
                            allowTypes="/(\.|\/)(gif|jpe?g|png)$/"
                            auto="true"/>
        <p:growl id="messages"  showDetail="true" />

And here is my backing bean: 这是我的支持豆:

 import java.util.LinkedList;
 import java.util.List;
 import org.primefaces.event.FileUploadEvent;
 import org.primefaces.model.UploadedFile;
 import javax.faces.bean.ManagedBean;
 import javax.faces.bean.SessionScoped;
 import javax.faces.event.ActionEvent;

 @ManagedBean
 @SessionScoped
public class FileUpload {  

private UploadedFile uploadedFile;

List<UploadedFile> files = new LinkedList<UploadedFile>();

public FileUpload() 
{}

public List<UploadedFile> getFiles() {
    System.out.println( "< getFiles > ");
    return files;
}

public void setFiles(List<UploadedFile> files) {
    System.out.println( "< setFiles > ");
    this.files = files;
}

public void addFileToAttachment(FileUploadEvent event)
{
    System.out.println("addfile");
    System.out.println( "upload > " + event.getFile().getFileName());
    files.add(event.getFile());
}

public UploadedFile getUploadedFile() {
    System.out.println( "< getFile > ");
    return uploadedFile;
}

public void setUploadedFile(UploadedFile uploadedFile) {
    System.out.println( "< setFile > ");
    this.uploadedFile = uploadedFile;
}

public void listener(ActionEvent ae)
{
    System.out.println("listener");
}



public void insert()
{
    System.out.println( "insert");
    if(uploadedFile !=null)
    {
        System.out.println( "  > " + uploadedFile.getFileName());
    }
}

} 

I see the upload interface on my webpage and the files seem uploaded, but none of the listeners/setters println is printed on my output. 我在网页上看到了上传界面,并且文件似乎已上传,但是没有侦听器/设置器println打印在我的输出中。 A regular commandbutton with its listener in my FileUpload bean fires it normally. FileUpload Bean中带有侦听器的常规commandbutton会正常触发它。

Using Primefaces 4.0, I didn't add the FileUpload filter in my web.xml file (I also tried to add it just in case, but it didn't work any better). 使用Primefaces 4.0,我没有在web.xml文件中添加FileUpload过滤器(为了以防万一,我也尝试添加它,但效果不佳)。

Also, commons-fileupload-1.3.1.jar and commons-io-2.4.jar are in the libraries of my project. 另外,commons-fileupload-1.3.1.jar和commons-io-2.4.jar在我的项目的库中。

Does anyone have an idea of where my problem could be ? 有谁知道我的问题可能在哪里?

Thank you 谢谢

After multiple checks, I realized that I used javax.faces-2.1 instead of 2.2. 经过多次检查,我意识到我使用javax.faces-2.1而不是2.2。 Combined with javax.servlet API and still in Primefaces 4.0 my problem now seems solved. 结合javax.servlet API并仍在Primefaces 4.0中,我的问题现在似乎已解决。

In your code fileUploadListener="#{userWizard.fu.addFileToAttachment}" , but you just post FileUpload.java . 在您的代码fileUploadListener="#{userWizard.fu.addFileToAttachment}" ,但是您只发布了FileUpload.java I did not see your userWizard bean and addFileToAttachment listener method. 我没有看到您的userWizard bean和addFileToAttachment侦听器方法。 But, try as below 但是,尝试如下

Configure web.xml configuration as below : 配置web.xml配置,如下所示:

<filter>
    <filter-name>PrimeFaces FileUpload Filter</filter-name>
    <filter-class>org.primefaces.webapp.filter.FileUploadFilter</filter-class>
    <init-param>
        <param-name>thresholdSize</param-name>
        <param-value>51200</param-value>
    </init-param>
</filter>
<filter-mapping>
    <filter-name>PrimeFaces FileUpload Filter</filter-name>
    <servlet-name>Faces Servlet</servlet-name>
</filter-mapping>

Your h:form must be like <h:form enctype="multipart/form-data"> . 您的h:form必须类似于<h:form enctype="multipart/form-data">

For multiple upload example : 对于多个上传示例:

multipleupload.xthml multipleupload.xthml

<h:form enctype="multipart/form-data">
        <p:fileUpload fileUploadListener="#{MultipleUploadActionBean.handleProposalAttachment}"  
                    mode="advanced" multiple="true" sizeLimit="3000000"
                    allowTypes="/(\.|\/)(gif|jpe?g|png)$/" id="attachment"/>
</h:form>

MultipleUploadActionBean.java MultipleUploadActionBean.java

@ManagedBean(name = "MultipleUploadActionBean")
@ViewScoped
public class MultipleUploadActionBean {
    private List<UploadedFile> uploadFileList = new ArrayList<UploadedFile>();

    public void handleProposalAttachment(FileUploadEvent event) {
        UploadedFile uploadedFile = event.getFile();
        uploadFileList.add(uploadedFile);
    }
}

For single upload example : 对于单个上传示例:

singleupload.xthml singleupload.xthml

<h:form enctype="multipart/form-data">
    <p:fileUpload value="#{SingleUploadActionBean.uploadedFile}" mode="simple"/>
    <p:commandButton value="Submit" ajax="false" actionListener="#{SingleUploadActionBean.upload}"/> 
</h:form>

SingleUploadActionBean.java SingleUploadActionBean.java

@ManagedBean(name = "SingleUploadActionBean")
@ViewScoped
public class SingleUploadActionBean {
    private UploadedFile uploadedFile;
    //getter & setter

    public void upload() {
        //your operation
    }
}   

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

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