Upload Files with JSF and MyFaces
Pages: 1, 2, 3, 4, 5, 6
The faces-config.xml File
The JSF configuration file defines the backing bean in the
request scope and specifies a navigation rule:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE faces-config PUBLIC
"-//Sun Microsystems, Inc.//DTD JavaServer Faces Config 1.1//EN"
"http://java.sun.com/dtd/web-facesconfig_1_1.dtd">
<faces-config>
<managed-bean>
<managed-bean-name>myBean</managed-bean-name>
<managed-bean-class>
com.devsphere.articles.jsfupload.MyBean
</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
</managed-bean>
<navigation-rule>
<from-view-id>/MyForm.jsp</from-view-id>
<navigation-case>
<from-outcome>OK</from-outcome>
<to-view-id>/MyResult.jsp</to-view-id>
</navigation-case>
</navigation-rule>
</faces-config>
The MyResult.jsp Page
This web page displays some information about the uploaded file and the hash value:
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h"%>
<f:view>
<h:panelGrid columns="2" border="0" cellspacing="5">
<h:outputText value="FileName:"/>
<h:outputText value="#{myBean.myFile.name}"/>
<h:outputText value="FileSize:"/>
<h:outputText value="#{myBean.myFile.size}"/>
<h:outputText value="Param:"/>
<h:outputText value="#{myBean.myParam}"/>
<h:outputText value="Result:"/>
<h:outputText value="#{myBean.myResult}"/>
</h:panelGrid>
</f:view>
The displayed file name may actually contain the full path in the client's file system, as provided by the web browser:

Figure 2. The output produced by the result page
Summary
There are many cases when users need to upload files through their browsers, but there is no ideal way for handling these files on the server side. Keeping the files' content in memory is acceptable only for small files, while storing the uploaded content in temporary files complicates the situation. MyFaces lets you choose the solution that is good for your application, but this framework has some minor problems. It doesn't let you delete the temporary files when you don't need them anymore, the file names are sometimes file paths, and there are no warnings when users attempt to upload files that are too large. These can be fixed, since the source code is available, and this article indicates where you can improve the code of MyFaces. For many applications, however, you might find MyFaces usable without any changes. The samples of this article have been tested with JSF 1.1.01, MyFaces 1.0.9, and Commons File Upload 1.0.
Resources
- Sample code for this article
- JavaServer Faces
- Apache MyFaces (see also: CodeZoo: MyFaces)
- Oracle ADF Faces
- Commons FileUpload
Andrei Cioroianu is the founder of Devsphere and an author of many Java articles published by ONJava, JavaWorld, and Java Developer's Journal.
|
Related Reading JavaServer Faces |
Return to ONJava.com.
-
404
2009-09-30 10:24:06 sonia_p [View]
-
404
2009-09-30 10:23:24 sonia_p [View]
-
jsf page
2009-09-30 09:23:11 sonia_p [View]
-
Inform the user that the size exceeded
2009-02-26 11:42:27 BenoƮt de CHATEAUVIEUX [View]
-
file not getting uploaded
2008-04-04 15:22:41 Meraj Zia [View]
-
Where is this TLD file
2008-03-12 23:48:30 mostafa_saad [View]
-
Important
2007-10-12 00:46:16 Nicos109 [View]
-
Using Tomahawk in a portal
2007-10-04 04:08:19 Stewart.Cambridge [View]
-
can we get data from the database to populate the list box
2007-04-12 02:51:40 14dr_19 [View]
-
can we get data from the database to populate the list box
2007-04-12 02:51:38 14dr_19 [View]
-
can we get data from the database to populate the list box
2007-04-12 02:51:35 14dr_19 [View]
-
problem
2007-03-22 02:29:34 14dr_19 [View]
-
problem
2007-03-22 02:29:32 14dr_19 [View]
-
problem
2007-03-22 02:29:30 14dr_19 [View]
-
problems
2007-02-26 07:51:40 AlexDafinei [View]
-
Upgraded version problem
2006-08-03 05:04:28 radha_thegr8 [View]
-
uploaded files dir
2006-07-02 07:59:02 mvpc [View]
-
display message if file was too big
2005-10-04 07:18:31 acp [View]
