This question already has answers here:
How to use PrimeFaces p:fileUpload? Listener method is never invoked or UploadedFile is null / throws an error / not usable
(11 answers)
Closed 2 years ago.
I'm using,
GlassFish 4.0
JSF 2.2
Mojarra 2.2.0
Primefaces 3.5
I have this fileUpload component on an XHTML page.
<h:form enctype="multipart/form-data">
<p:fileUpload id="txtCatImage"
value="#{testManagedBean.uploadedFile}"
mode="advanced"
sizeLimit="100000"
multiple="false"
showButtons="true"
allowTypes="/(\.|\/)(gif|jpe?g|png)$/"
fileUploadListener="#{testManagedBean.fileUploadListener}"/>
<p:message for="txtCatImage" showSummary="false"/>
<p:commandButton id="btnSubmit"
actionListener="#{testManagedBean.insert}"
icon="ui-icon-check" value="Save"/>
</h:form>
This is the corresponding managed bean.
#ManagedBean
#ViewScoped
public final class TestManagedBean implements Serializable {
private UploadedFile uploadedFile;
private static final long serialVersionUID = 1L;
public TestManagedBean() {}
public UploadedFile getUploadedFile() {
return uploadedFile;
}
public void setUploadedFile(UploadedFile uploadedFile) {
this.uploadedFile = uploadedFile;
}
public void fileUploadListener(FileUploadEvent event) {
uploadedFile = event.getFile();
System.out.println("fileUploadListener invoked.");
}
public void insert() {
if (uploadedFile != null) {
System.out.println(uploadedFile.getFileName());
} else {
System.out.println("The file object is null.");
}
}
}
The file upload listener as mentioned - fileUploadListener() is never invoked.
The file upload filter is mapped in web.xml as follows.
<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>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.jsf</url-pattern>
</servlet-mapping>
I have added Apache-commons-fileupload and Apache-commons-io to the classpath. The same thing works in my earlier project with Spring / JSF.
I can't see what I'm missing here. Does it have to do something with the version of the server?
It's most probably because Primefaces 4 is the only one compatible with the latest JSF standard present in Glassfish 4 (Java EE 7). Primefaces 3.5 works with Glassfish 3.1.2.2 (Java EE 6)
There were more people having this issue.
Monday 16th of September will be launched Primefaces 4.0 RC1, so you should try with that if you really want GF 4.
Edit:
Reference: Glassfish 4, JSF 2.2 and PrimeFaces FileUploadEvent not working together
The user can decide by itself if RC is good enough for him or not. I have just suggested it to him, because that's the only way at the moment.
If you use Primefaces upload, you have to take care the following situation.
Don't use ajax request, that mean, you have to put ajax="false" attribute in your <p:commandButton...>.Otherwise, use <h:commandButton>.
If you would like to display dynamic image, the scope of the backing bean must be Session Scope. Otherwise, you have to write the file in the temporary storage and use this file path to display dynamic image.
Reference is here
Related
I am using JSF 2.2 framework using primefaces, but I am unable to create multiple files import .I have tried using the lots of option but it doesn't work
public static Collection getAllParts(Part part) throws ServletException, IOException {
HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
return request.getParts().stream().filter(p -> part.getName().equals(p.getName())).collect(Collectors.toList());
}
public void submit() throws ServletException, IOException {
for (Part part : getAllParts(file)) {
String fileName = part.getSubmittedFileName();
InputStream fileContent = part.getInputStream();
// ...
//
// E.g. https://stackoverflow.com/q/14211843/157882
}
}
But it is still not working.Moreover HttpServletRequest request is returning null value. Please help me in resolving this issue
Use jsf and Primefaces instead of the servlet request.
e.g.
In your web.xml
<context-param>
<param-name>primefaces.UPLOADER</param-name>
<param-value>auto|native|commons</param-value>
</context-param>
<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>
In your pom
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.4</version>
</dependency>
In your .xhtml :
<h:form id="uploadingForm" enctype="multipart/form-data">
<p:fileUpload listener="#{yourbean.handleFileUpload}"
mode="advanced" dragDropSupport="false"
multiple="true" update="messages" sizeLimit="100000" fileLimit="3" />
</h:form>
In your bean:
private List<UploadedFile> uploadedFiles;
public void handleFileUpload(FileUploadEvent event) {
uploadedFiles.add(event.getFile());
}
now can you process your files on the uploadedFiles list
you can even check the documentation
I try to make a simple fileupload so:
<h:form enctype="multipart/form-data">
<p:fileUpload value="#{catastrofes.file}" mode="simple"></p:fileUpload>
<p:separator/>
<h:commandButton value="Dummy Action" action="#{catastrofes.dummyAction}" ajax="false"></h:commandButton>
</h:form>
But when I submit, get file = null and raise NullPointerException, here a similar question, but the answer don't work for me, any ideas?
Here my managed bean:
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import org.primefaces.model.UploadedFile;
#ManagedBean(name="catastrofes")
#RequestScoped
public class CatastrofesBean {
private UploadedFile file;
public String dummyAction() {
System.out.println("Uploaded File Name Is :: " + file.getFileName() + " :: Uploaded File Size :: " + file.getSize());
return "";
}
public UploadedFile getFile() {
return file;
}
public void setFile(UploadedFile file) {
this.file = file;
}
}
And my web.xml:
<context-param>
<param-name>primefaces.UPLOADER</param-name>
<param-value>auto|native|commons</param-value>
</context-param>
<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>
Thanks!
According to primefaces_user_guide
auto: This is the default mode and PrimeFaces tries to detect the best method by checking the
runtime environment, if JSF runtime is at least 2.2 native uploader is selected, otherwise commons.
native: Native mode uses servlet 3.x Part API to upload the files and if JSF runtime is less than 2.2
and exception is being thrown.
commons: This option chooses commons fileupload regardless of the environment, advantage of
this option is that it works even on a Servlet 2.5 environment.
You should choose only one option not paste all those three options.
If you have commons-fileupload library in your project, I suggest you to choose commons.
in web.xml
<context-param>
<param-name>primefaces.UPLOADER</param-name>
<param-value>commons</param-value>
</context-param>
xhtml
<h:form enctype="multipart/form-data">
<p:growl id="messages" showDetail="true" />
<p:fileUpload value="#{fileUploadView.file}" mode="simple" />
<br /> <br />
<h:commandButton value="Dummy Action"
action="#{fileUploadView.upload}">
</h:commandButton>
</h:form>
managedbean
#ManagedBean
#RequestScoped
public class FileUploadView {
private UploadedFile file;
public UploadedFile getFile() {
return file;
}
public void setFile(UploadedFile file) {
this.file = file;
}
public void upload() {
System.out.println("Uploaded File Name Is :: " +
file.getFileName() +
" :: Uploaded File Size :: " + file.getSize());
if(file != null) {
FacesMessage message = new FacesMessage("Succesful", file.getFileName() + " is uploaded.");
FacesContext.getCurrentInstance().addMessage(null, message);
}
}
}
I am using the Primefaces 5.0 file uploaded with the Apache Commons Uploader. The control appears on the web page and I can select a file, but when I click a button to action it the file object in the controller has not been set.
The web page:
<h:form id="master">
<p:panelGrid columns="2">
<p:fileUpload value="#{controller.file}" mode="simple"/>
<p:commandButton value="Upload"
ajax="false" actionListener="# {controller.uploadFile}" />
</p:panelGrid>
Controller:
#ManagedBean
#ViewScoped
public class Controller implements Serializable {
private UploadedFile file;
public UploadedFile getFile() {
return file;
}
public void setFile(UploadedFile file) {
this.file = file;
}
public void uploadFile() {
if(file != null) {
//Doesn't get here because file is null
}
}
web.xml
<context-param>
<param-name>primefaces.UPLOADER</param-name>
<param-value>common</param-value>
</context-param>
<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>
I tried setting the File Uploader to "auto" in the web.xml, but then I get multi-part exceptions which I couldn't resolve.
You miss to use enctype="multipart/form-data" in <h:form>. Change as below
<h:form enctype="multipart/form-data">
...
</h:form>
What does enctype='multipart/form-data' mean?
This question already has answers here:
How to use PrimeFaces p:fileUpload? Listener method is never invoked or UploadedFile is null / throws an error / not usable
(11 answers)
Closed 3 years ago.
I am using p:fileUpload but the fileUploadListener is not getting invoked if I use mode="simple" . Is there any way to use fileUploadListener in simple mode.
<p:fileUpload id ="uploading"
fileUploadListener="#{workflowActionTemplate.handleFileUpload}"
mode="simple"
update="messages"
sizeLimit="100000"
allowTypes="/(\.|\/)(gif|jpe?g|png|pdf)$/"
multiple="true"/>
Prime faces : 3.2
I have done following configuration, please let me know if I am missing anything.
web.xml:
<!-- File Upload filter -->
<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>
<dispatcher>FORWARD</dispatcher>
</filter-mapping>
pom.xml:
<!-- Dependancy for file upload -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.primefaces</groupId>
<artifactId>primefaces</artifactId>
<version>3.2</version>
</dependency>
listener Method:
public void handleFileUpload(FileUploadEvent event) {
FacesMessage msg = new FacesMessage("Succesful", event.getFile().getFileName() + " is uploaded."); System.out.println("msg : "+ msg);
uploadedFile = event.getFile();
}
--
If I use value attribute instead of fileUploadListener and if I don't upload the file, then
the fileUpload attribute not getting set and so it gives following error.
javax.faces.component.UpdateModelException: javax.el.ELException: /search/workflowAction.xhtml #181,104 value="#{workflowActionTemplate.uploadedFile}": Can't set property 'uploadedFile' of type 'org.primefaces.model.UploadedFile' on class 'com.principal.nq.statements.search.WorkflowActionTemplate$$EnhancerByCGLIB$$6ebcb7eb' to value ''
Update
As fileUploadListener is not working I was also trying to use ajax call in the following way to update the file value. But f:ajax is not able to execute Primefaces p:fileUpload component. I also tried with p:ajax but that is also not working.
<p:fileUpload id="uploading"
value="#{workflowActionTemplate.uploadedFile}"
mode="simple"
update="messages"
sizeLimit="100000"
allowTypes="/(\.|\/)(gif|jpe?g|png|pdf)$/"
auto="true"/>
<p:growl id="messages" showDetail="true"/>
<h:commandButton id="uploadDocument" styleClass="continuebutton" value="#{msg.upload}" action="#{workflowActionTemplate.uploadParticipantCustomDoc}">
<f:ajax execute="uploading" render="uploadDocumentDlg" onevent="onAjaxUploadCustomDoc"/>
</h:commandButton>
Please follow these steps to get your code perfectly fine.
in the XHTML file.
<p:fileUpload id="choose" validator="#{controllerClass.validateFile}" multiple="false" allowTypes="/(\.|\/)(gif|jpe?g|png)$/" value="#{controllerclass.uploadedfile}" required="true" mode="simple"/>
<p:commandButton ajax="false" id="saveBtn" update="errBrand,pnl" value="Save Brand" action="#{controllerClass.uploadFile()}" />
in the web.xml define following filter and servlet.
<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>
in the controller class you need to define method body like this
use import org.primefaces.model.UploadedFile;
private UploadedFile uploadedfile;
if you want to define validation method for uploaded file you can write it like this
public void validateFile(FacesContext ctx,
UIComponent comp,
Object value) {
List<FacesMessage> msgs = new ArrayList<FacesMessage>();
UploadedFile file = (UploadedFile)value;
int fileByte = file.getContents().length;
if(fileByte > 15360){
msgs.add(new FacesMessage("Too big must be at most 15KB"));
}
if (!(file.getContentType().startsWith("image"))) {
msgs.add(new FacesMessage("not an Image file"));
}
if (!msgs.isEmpty()) {
throw new ValidatorException(msgs);
}
}
This question already has answers here:
How to use PrimeFaces p:fileUpload? Listener method is never invoked or UploadedFile is null / throws an error / not usable
(11 answers)
Closed 7 years ago.
I am facing a problem with <p:fileUpload> of PrimeFaces. I created a Facelet page to upload a file as below:
<h:form id="welcomeForm">
<p:fileUpload value="#{fileUploadController.uploadedFile}" mode="simple" />
<h:commandButton value="Submit" action="#{fileUploadController.submit}" />
<h:message for="welcomeForm" />
</h:form>
And a backing bean as below:
public class FileUploadController {
private UploadedFile uploadedFile;
public FileUploadController() {
}
public UploadedFile getUploadedFile() {
return uploadedFile;
}
public void setUploadedFile(UploadedFile uploadedFile) {
this.uploadedFile = uploadedFile;
}
public void submit() {
// Get information you from the uploaded file
System.out.println("Uploaded file name : " + uploadedFile);
}
}
When I click the Submit button, the method submit() is called but it the result is as below :
INFO: Uploaded file name : null
How is this caused and how can I solve it?
Please read the <p:fileUpload> chapter of the PrimeFaces User Guide.
Getting started with FileUpload
First thing to do is to configure the fileupload filter which parses the multipart request. FileUpload filter should map to Faces Servlet.
<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>
Simple File Upload
Simple file upload mode works in legacy mode with a file input whose value should be an
UploadedFile instance.
<h:form enctype="multipart/form-data">
<p:fileUpload value="#{fileBean.file}" mode="simple" />
<p:commandButton value="Submit" ajax="false"/>
</h:form>
import org.primefaces.model.UploadedFile;
public class FileBean {
private UploadedFile file;
//getter-setter
}
Please note the enctype="multipart/form-data" attribute of the form. This is mandatory for HTML in order to be able to send files to the server. The filter is mandatory for JSF in order to extract the data from multipart/form-data requests. Without either of them, either the command action won't be invoked, or all properties will be null.
I think that the problem is that the simple upload doesn't support ajax.
you should add ajax="false":
<h:form id="welcomeForm">
<p:fileUpload value="#{fileUploadController.uploadedFile}" mode="simple" />
<h:commandButton value="Submit" action="#{fileUploadController.submit}" ajax="false" />
<h:message for="welcomeForm" />
</h:form>
Or use the primefaces autouploader.
I have the same issue, I have solved it by adding
<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>
like BalusC said.
but adding this :
<!-- JSF mapping -->
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- Map these files with JSF -->
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.jsf</url-pattern>
</servlet-mapping>
Because by default in J2EE 6 this part is optional JSF 2.0 Servlet activates automatically when the WEB-INF/faces-config.xml descriptor is present.
But it necessary to active correctly PrimeFaces Filter
Jboss 6.1.0.Final / PrimeFaces 3.0.RC2
Try This:
<h:form id="welcomeForm" enctype="multipart/form-data">
<p:fileUpload value="#{fileUploadController.uploadedFile}" mode="simple" />
<h:commandButton value="Submit" action="#{fileUploadController.submit}" />
<h:message for="welcomeForm" />
</h:form>
(enctype="multipart/form-data" is very important for file upload.)
And:
public class FileUploadController {
private UploadedFile uploadedFile;
public FileUploadController() {
}
public UploadedFile getUploadedFile() {
return uploadedFile;
}
public void setUploadedFile(UploadedFile uploadedFile) {
this.uploadedFile = uploadedFile;
}
public void submit() {
// Get information you from the uploaded file
System.out.println("Uploaded file name : " + uploadedFile.getFileName());
}
}
uploadedFile.getFileName() used to get the file name.
I had the same issue and just a very little THING saved me:
I did all the internet says about the prime faces and file upload thing
After applying the needed filter mappings and needed dependencies, I saw that dispacher information must also be added to the filter mappings:
<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>FacesServlet</servlet-name>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
</filter-mapping>
<servlet>
<servlet-name>FacesServlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>FacesServlet</servlet-name>
<url-pattern>/faces/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>FacesServlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
Maybe this very little configuration can save lives just like mine, good luck!