ONJava.com -- The Independent Source for Enterprise Java
oreilly.comSafari Books Online.Conferences.


AddThis Social Bookmark Button

Upload Files with JSF and MyFaces
Pages: 1, 2, 3, 4, 5, 6

The API provided by the Commons File Upload package gives you access to the parsed from data, but the getParameter() and getParameterValues() methods of the servlet request won't work. This is a problem, since these two methods are called by the standard JSF components that run behind regular input fields, checkboxes, radio buttons, and lists. The Servlets API provides two features (filters and request wrappers) that can be used to solve this problem. The next section describes how Apache MyFaces implements a filter that adds the much-needed support for file uploading without breaking the existing JSF components. In addition, MyFaces provides APIs for JavaBeans along with a custom JSF component that renders the <input type="file"> elements.

Configuring JSF and MyFaces Extensions

Currently, there is a main implementation of the JSF specification called JSF Reference Implementation (RI) and there is another one provided by Apache, which is known as MyFaces. There might be other JSF implementations, but JSF RI and MyFaces are the most popular. Many developers prefer the former because it's the "official" implementation from Sun, but MyFaces has some interesting extensions, such as the support for uploading files. You can use the MyFaces extensions together with the JSF RI from Sun if you want. You just have to put the myfaces-extensions.jar file together with the JAR files of JSF RI and commons-fileupload-1.0.jar in the WEB-INF/lib directory of your web application. Here are the JAR files that you need:

JSF 1.1 RI jsf-api.jar
JSTL 1.1 RI jstl.jar
MyFaces Extensions myfaces-extensions.jar
Apache Commons
(used by JSF and
MyFaces Extensions)

A class named MultipartRequestWrapper, which can be found in the org.apache.myfaces.component.html.util package, creates the bridge between MyFaces and Commons File Upload. This class extends HttpServletRequestWrapper, overriding the getParameterMap(), getParameterNames(), getParameter(), and getParameterValues() methods so that they can work properly with the multipart/form-data encoding. In addition, MultipartRequestWrapper provides two new methods, named getFileItem() and getFileItems(), that give you access to the uploaded files through the org.apache.commons.fileupload.FileItem interface.

The MyFaces ExtensionsFilter from the org.apache.myfaces.component.html.util package creates the MultipartRequestWrapper instance when it detects the multipart/form-data encoding. Therefore, you shouldn't be concerned about the parsing of the form data, but it's useful to know how this is implemented in case you want to change the way uploaded files are handled. In a typical application, you just have to configure the ExtensionsFilter in the web.xml descriptor of your web application so that it can intercept the HTTP requests before the FacesServlet of JSF:

<?xml version="1.0" encoding="UTF-8"?>

    "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"










Pages: 1, 2, 3, 4, 5, 6

Next Pagearrow