I'm doing a webapplication running on an Apache Tomcat 7 using Primefaces 4.0. Trying to realize an upload interface similar to the Primefaces ShowCase, the upload interface doesn't fire any function in the backing bean:
Here's what I included directly in my body (I also tried without "multiple", "value", advanced mode, "actionListener" attributes)
<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. A regular commandbutton with its listener in my FileUpload bean fires it normally.
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).
Also, commons-fileupload-1.3.1.jar and commons-io-2.4.jar are in the libraries of my project.
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. Combined with javax.servlet API and still in Primefaces 4.0 my problem now seems solved.
In your code fileUploadListener="#{userWizard.fu.addFileToAttachment}"
, but you just post FileUpload.java
. I did not see your userWizard
bean and addFileToAttachment
listener method. But, try as below
Configure web.xml
configuration as below :
<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">
.
For multiple upload example :
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
@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
<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
@ManagedBean(name = "SingleUploadActionBean")
@ViewScoped
public class SingleUploadActionBean {
private UploadedFile uploadedFile;
//getter & setter
public void upload() {
//your operation
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.