FileUpload with JSF 2 and Servlet 3.0

29 Dec
2009

As I had serious problems with FileUpload and the existing “solutions”, mainly Tomahawk, MyFaces, RichFaces, PrimeFaces, etc. which are all not 100% ready for JSF 2.

I created a taglib based on the code of BalusC.
You can find the Taglib on Github

The source code is based on these two posts:

  1. Uploading files in Servlet 3.0
  2. Uploading files with JSF 2.0 and Servlet 3.0

Simply check out the code with git

1
git clone http://github.com/domdorn/fileUploadServlet3JSF2.git

then install the taglib with

1
mvn clean compile install

and import it into your maven project like this

net.balusc
fileUploadServlet3JSF2
1.0-SNAPSHOT

You can then use the taglib in your Facelets files like this:

Upload.xhtml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"
	xmlns:h="http://java.sun.com/jsf/html"
	xmlns:f="http://java.sun.com/jsf/core"
	xmlns:ui="http://java.sun.com/jsf/facelets"
	xmlns:hh="http://balusc.net/jsf/html"
>
<h:head>
	<title>FileUploadTest</title>
</h:head>
<body>
 
<h:form prependId="false">
 
	<h:messages globalOnly="false" id="messages"/>
 
	<h:panelGrid columns="3">
 
			<label for="someText">
				SomeText:
			</label>
 
			<h:inputText id="someText" value="#{uploadBean.someText}" required="true">
				<f:ajax event="blur" render="someText someTextMessage" execute="@this"/>
			</h:inputText> <h:message for="someText" id="someTextMessage"/>
 
			<label for="filenameText">
			Filename:
			</label>
 
			<h:inputText id="filenameText" value="#{uploadBean.filename}" required="true">
				<f:ajax event="blur" render="filenameText filenameTextMessage" execute="@this" />
			</h:inputText>
			<h:message for="filenameText" id="filenameTextMessage"/>
	</h:panelGrid>
	</h:form>
 
	<h:form enctype="multipart/form-data" prependId="false">
	<h:panelGrid columns="3">
		<h:outputLabel for="uploadedFile" rendered="#{empty uploadBean.file}">
				Input File:
		</h:outputLabel>
		<hh:inputFile id="uploadedFile" value="#{uploadBean.file}" rendered="#{empty uploadBean.file}">
			<f:validator validatorId="fileValidator"/>
		</hh:inputFile>
		<h:message for="uploadedFile"/>
	</h:panelGrid>
	<h:commandButton value="submit" action="#{uploadBean.submit}" />
</h:form>
</body>
</html>

The bean:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package com.dominikdorn.simpleFileUpload.beans;</code>
 
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.bean.SessionScoped;
import javax.faces.bean.ViewScoped;
import java.io.File;
 
@ViewScoped
@ManagedBean
public class UploadBean {
 
private File file;
private String filename;
 
public String getFilename() {
return filename;
}
 
public void setFilename(String filename) {
System.out.println("binding filename");
this.filename = filename;
}
 
public String submit()
{
System.out.println("calling submit");
 
if(file != null)
{
this.filename = file.getName();
}
 
System.out.println("processed");
// do what you want with the file
return "yeah";
}
 
public UploadBean() {
}
 
public File getFile() {
return file;
}
 
public void setFile(File file) {
this.file = file;
}
 
private String someText;
 
public String getSomeText() {
return someText;
}
 
public void setSomeText(String someText) {
System.out.println("binding someText");
this.someText = someText;
}
}

8 Responses to FileUpload with JSF 2 and Servlet 3.0

Avatar

Ray

March 1st, 2010 at 04:59

thanks !

( you could add a jar for download though, not all want to install git and maven 🙂

Avatar

Peter

March 5th, 2010 at 19:56

Nice, I took a different approach, because I do not have maven building my projects. So I hacked a baseclass to call instead

http://ironicprogrammer.blogspot.com/2010/03/file-upload-in-jsf2.html

Avatar

J

March 23rd, 2010 at 21:09

I tried this out with glassfish3 final, and I get no errors, but it seems like UploadBean.submit is never called. I can see with firebug that the form definitely submits the file, the lines “calling submit” and “processes” are never logged however. Any ideas?

Avatar

imran pariyani

March 30th, 2010 at 17:13

Avatar

Kawalya

May 7th, 2010 at 16:03

Hi, I have tried this out with the jar, but when i use its file input tag i only see the id attribute; all other attributes like value are not defined: The system flags an error claiming” The value attribute is not defined in the component interface”.

Any help please?
Thanks.

Avatar

figo

August 6th, 2010 at 11:16

yes,it dosen’t work.
i use glassfish3.0.1

Avatar

Derek

January 3rd, 2011 at 18:22

I’m no maven expert, but here is the errors I get when running the commands above

[INFO] Scanning for projects…
[INFO] ————————————————————————
[INFO] Building jsf2uuploadpload
[INFO] task-segment: [clean, compile]
[INFO] ————————————————————————
[INFO] [clean:clean {execution: default-clean}]
[INFO] Deleting file set: /home/derek/fileUploadServlet3JSF2/target (included: [**], excluded: [])
[INFO] [resources:resources {execution: default-resources}]
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 2 resources
Downloading: http://repo1.maven.org/maven2/com/sun/faces/jsf-impl/2.0.1/jsf-impl-2.0.1.pom
[INFO] Unable to find resource ‘com.sun.faces:jsf-impl:pom:2.0.1’ in repository central (http://repo1.maven.org/maven2)
Downloading: http://repo1.maven.org/maven2/com/sun/faces/jsf-impl/2.0.1/jsf-impl-2.0.1.jar
[INFO] Unable to find resource ‘com.sun.faces:jsf-impl:jar:2.0.1’ in repository central (http://repo1.maven.org/maven2)
[INFO] ————————————————————————
[ERROR] BUILD ERROR
[INFO] ————————————————————————
[INFO] Failed to resolve artifact.

Missing:
———-
1) com.sun.faces:jsf-impl:jar:2.0.1

Try downloading the file manually from the project website.

Then, install it using the command:
mvn install:install-file -DgroupId=com.sun.faces -DartifactId=jsf-impl -Dversion=2.0.1 -Dpackaging=jar -Dfile=/path/to/file

Alternatively, if you host your own repository you can deploy the file there:
mvn deploy:deploy-file -DgroupId=com.sun.faces -DartifactId=jsf-impl -Dversion=2.0.1 -Dpackaging=jar -Dfile=/path/to/file -Durl=[url] -DrepositoryId=[id]

Path to dependency:
1) net.balusc:fileUploadServlet3JSF2:jar:1.0-SNAPSHOT
2) com.sun.faces:jsf-impl:jar:2.0.1

———-
1 required artifact is missing.

for artifact:
net.balusc:fileUploadServlet3JSF2:jar:1.0-SNAPSHOT

from the specified remote repositories:
central (http://repo1.maven.org/maven2)

[INFO] ————————————————————————
[INFO] For more information, run Maven with the -e switch
[INFO] ————————————————————————
[INFO] Total time: 1 second
[INFO] Finished at: Mon Jan 03 11:17:42 EST 2011
[INFO] Final Memory: 12M/189M
[INFO] ————————————————————————

Avatar

James

January 27th, 2011 at 16:42

Do I need a Filter?

Comment Form

top