1

I'm new to primefaces and I'm trying to implement fileUpload component in my project. I have opened documentation but it is not clear for me and my implementation doesn't work.

First line of documentation says:

"First thing to do is to configure the fileupload filter which parses the multipart request. FileUpload filter should map to Faces Servlet."

There is recomendation to put this code:

<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>

into web.xml descriptor

First problem: "... filter which parses the multipart request". I understand that this means if I would like to upload more than 1 file I should use that code(I'm not sure). So I thought I should miss that however in most of examples I found in the internet this code should be implemented.

Second problem: When I try to implement filter in my web.xml file:

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">

<display-name>Archetype Created Web Application</display-name>

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/classes/configuration-context.xml</param-value>
</context-param>

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>

<filter>
    <filter-name>springSecurityFilterChain</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
    <filter-name>springSecurityFilterChain</filter-name>
    <url-pattern>/*</url-pattern>
    <dispatcher>FORWARD</dispatcher>
    <dispatcher>REQUEST</dispatcher>
    <dispatcher>INCLUDE</dispatcher>
    <dispatcher>ERROR</dispatcher>
</filter-mapping>

<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>

<listener>
    <listener- class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>


<!-- JSF 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>*.jsf</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>facesServlet</servlet-name>
    <url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>facesServlet</servlet-name>
    <url-pattern>*.faces</url-pattern>
</servlet-mapping>

<context-param>
    <param-name>javax.faces.PROJECT_STAGE</param-name>
    <param-value>Development</param-value>
</context-param>
<context-param>
    <param-name>javax.faces.DEFAULT_SUFFIX</param-name>
    <param-value>.xhtml</param-value>
</context-param>
<context-param>
    <param-name>primefaces.THEME</param-name>
    <param-value>redmond</param-value>
</context-param>

</web-app>

I get a lot of errors:

java.util.concurrent.ExecutionException: org.apache.catalina.LifecycleException: Failed to start component     [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/wals]]
at java.util.concurrent.FutureTask$Sync.innerGet(Unknown Source)
at java.util.concurrent.FutureTask.get(Unknown Source)
at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:1123)
at org.apache.catalina.core.StandardHost.startInternal(StandardHost.java:800)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)
at java.util.concurrent.FutureTask$Sync.innerRun(Unknown Source)
at java.util.concurrent.FutureTask.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: org.apache.catalina.LifecycleException: Failed to start component     [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/wals]]
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:154)
... 7 more
Caused by: java.lang.NoClassDefFoundError: org/apache/commons/fileupload/FileItemFactory
at java.lang.Class.getDeclaredFields0(Native Method)
at java.lang.Class.privateGetDeclaredFields(Unknown Source)
at java.lang.Class.getDeclaredFields(Unknown Source)
at org.apache.catalina.util.Introspection.getDeclaredFields(Introspection.java:106)
at org.apache.catalina.startup.WebAnnotationSet.loadFieldsAnnotation(WebAnnotationSet.java:261)
at org.apache.catalina.startup.WebAnnotationSet.loadApplicationFilterAnnotations(WebAnnotationSet.java:110)
at org.apache.catalina.startup.WebAnnotationSet.loadApplicationAnnotations(WebAnnotationSet.java:66)
at org.apache.catalina.startup.ContextConfig.applicationAnnotationsConfig(ContextConfig.java:405)
at org.apache.catalina.startup.ContextConfig.configureStart(ContextConfig.java:881)
at org.apache.catalina.startup.ContextConfig.lifecycleEvent(ContextConfig.java:369)
at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:119)
at org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:90)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5269)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
... 7 more
Caused by: java.lang.ClassNotFoundException: org.apache.commons.fileupload.FileItemFactory
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1714)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1559)
... 21 more

Third problem: This may be caused by 1st and 2nd problem but I'm not sure. If I abandon filter code and implement primefaces like that in xhtml file:

<h:form enctype="multipart/form-data">
<h:outputText value="Wybór zdjęcia: " />
<p:fileUpload value="#{fileUploadController.file}" mode="simple" />
<p:commandButton value="Submit" ajax="false"/>
</h:form>

Where fileUploadController bean is simply defined:

@Component
@Scope("session")
@ManagedBean
@SessionScoped
public class FileUploadController
{  
private UploadedFile file;

public UploadedFile getFile()
{
    return file;
}

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

I get null value for file field. My goal is to upload an image, change it into Blob and send to database. I would be very grateful for your help becase I can't continue my project without that part.

user2374573
  • 59
  • 1
  • 3
  • 11

4 Answers4

1

Looks like you might be missing some of the dependencies. There is a similar error on another thread which I have posted the link here for you. It basically suggests you need to add the jar files for commons-fileupload and commons-io. Hope this helps you resolve your problem.

Community
  • 1
  • 1
grepit
  • 21,260
  • 6
  • 105
  • 81
  • 2
    You are absolutely right. I added this libraries and there is no errors. Thank you very much. But there is still problem with null value of uploaded file. I read 2 factors may be the reason of the problem. First I should add enctype="multipart/form-data" tag. And the second reason may be problem with web.xml. So I updated this things to my post because I don't know what may be wrong. Could you help me? – user2374573 Jun 11 '13 at 11:06
  • @user2374573 Happy I could help and feel free to give it 1+ for that answer. Also, can you post the new error you are getting. So, I could help you more. I am not sure if you want open a new thread or just add it to the existing one. – grepit Jun 11 '13 at 13:17
  • I'm sorry but I'm below 15 points so I can't give you 1+. I do not get any errors. Just after uploading, file field still has null value and this is the problem. – user2374573 Jun 11 '13 at 15:56
  • try it now :) I think you should be able too. – grepit Jun 11 '13 at 16:04
  • 1
    Ok. I checked that and it definitely seem the setFile method is never invoked. – user2374573 Jun 11 '13 at 16:55
1

In my case problem was missing definition of Faces Servlet. As of Servlet 3.0, it is declared automatically by the container. Obviously servlet then might have some different name than FacesServlet, what breaks the FileUploadFilter definition.

Specifically - my problem was that listener was not called for advanced uploader, nor the file variable was set for the simple uploader.

https://docs.oracle.com/javaee/6/api/javax/faces/webapp/FacesServlet.html

Solution was to declare the Faces Servlet in web.xml (even it is being declared automatically).

My problem appeared on Jboss 7.1, with Primefaces 5.0. I tried following all the advices here, until I recognized that in fact I do not have the Faces Servlet defined. As other parts of the application were just fine with automated declaration, I spend a while searching for solution. Hope it would help anyone.

tomekpe
  • 11
  • 1
0

Problem was because of wrong servlet-name.

Changing:

<servlet-name>Faces Servlet</servlet-name>

to

<servlet-name>facesServlet</servlet-name>

solved the problem. It's a team project and I haven't noticed the name was changed so please be careful on that. Thanks for help.

user2374573
  • 59
  • 1
  • 3
  • 11
0

fileUpload and primefaces null value for uploaded file

Download commons-fileupload-1.2.1.jar, commons-io-2.0.1.jar and put WEB_INF/lib. Otherwise add dependency in pom.xml file. Your problem will solved.

Pravin
  • 103
  • 1
  • 14