7
At work, we have run into two issues several times: 1) We haves app that create PDFs, and we need our JSF apps to send that to the user, and 2) we need to be able to upload multiple files to one of our JSF apps. The solutions we’ve used have been less than exciting. For the first problem, we’d make the backing bean that coordinates the PDF creation (calling the service layer, basically) session-scoped, then have a hidden iframe on the result page whose source is a JSP that pulls the bean out the session (via Java code!) and sends the PDF to the browser in such a way that forces the user to save the file. For the upload issue, we’ve been using JUpload, which works fine, but, since it lives outside the JSF lifecycle, we have to do some interesting things to make it work. Luckily, my boss gave me time to create better solutions, resulting in the components and .
File Downloads
This component is really rather simple. Currently, it has these options:
- data
- mimeType
- fileName
- method
- text
- iframe
- height
- width
A few of these attributes need a little explanation:
- The
dataattribute is an EL expression that resolves to the content of the file to be downloaded. This data can be returned as abyte[],InputStream, or aByteArrayOutputStream. - The mime type needs to be specified as there is no easy way of determining the correct type short of guessing based on the file extension, adding a third party dependency, or writing a bunch of painful code myself. None of those are very appealing.
- The
methodis how the file is to be rendered: ‘inline’ (i.e., embedded in the page), or ‘download’ (i.e., force a Save As dialog). textis the text of the link tag for the download method.iframeis a boolean determining whether or not to use an iframe when embedding the file.
Usage is pretty simple:
which renders to this:

Multi-file Uploads
The second component is a bit trickier. Since HTML won’t allow more than one file selected per file input element, another method must be employed. Following JUpload’s lead, I implemented this part of the component in an applet. The applet allows the user to select a number of files and, when he’s ready, to submit them all in one batch. This component is a bit more complex, so we’ll spend more time on it, and we’ll start with the component parameters:
- type - The manner in which to render the applet: ‘full’ or ‘button’ (see below for details)
- fileHolder - The object into which uploaded files will be placed
- destinationUrl - The URL to which to navigate after an upload (see below)
- fileFilter - A string listing the extensions to allow, as well as a filter description (i.e., “Image Files|jpg,png,gif”)
- maxFileSize - The maximum size per file in bytes
- startDir - The directory in which to start looking for files
- buttonText - The text on the button if
typeis set to ‘button’ - height - The height of the rendered applet
- width - The width of the rendered applet
Some of those attributes needs some explaining:
- The
fileHolderis an object that implements thecom.iecokc.faces.components.upload.multifile.FileHolderinterface defined by the component. This class will be fetched by the component via theValueExpressiongiven in the tag. As each file is uploaded,FileHolder.addFile()is called, storing the file, in effect, in the backing bean (as it has a reference to the same object).The component provides an implementation of this interface,
FileHolderImpl, which simply takes theInputStreamfor the file, and stores it in aMap, indexed by filename. It is possible to write an implementation of the interface that reads each file in and writes it to a database, JCR repository, etc., depending on your application’s needs. - Once all of the files have been processed, the component retrieves the
destinationUrl, which, if aValueExpressionis used, gives the backing bean the chance to analyze the set of uploaded files and pick an appropriate destination URL based on application-specific needs. UNC paths are acceptable. - Care must be taken when using the
startDirattribute, as file systems paths are far from portable. For in-house applications, such as the one for which this component was developed, one can probably safely use this parameter. - In the extension part of the
fileFilterstring, only the extensions are need: no masks, periods, etc.
Here are a couple of sample usages.
Full Mode:
which renders like this:

Button Mode:
which renders like this:

What’s Next?
These components are extremely young (I “finished” the download component Monday and the upload component today), so the APIs and implementation need some more scrutiny by someone other than the guy who wrote them. That means, of course, that things may change, but I hope it won’t be anything major. Personally, they seem to solve effectively the problem which spawned them, so I see nothing that needs changing, but others likely will, so we’ll have to wait and see how all of that shakes out.
Enough already? Where can I get them?
That’s a very good question. As I noted, these were components developed for my company, but my boss has graciously given me permission to release them (in fact, he approached me about it). That leaves the question then, of exactly where/to whom to release them. I am currently in the middle of discussions on where these and my YUI components will live. They’ll either be rolled in to the newly opened JSF RI sandbox, or added to Ed Burns’ JSF-extensions project, and I’m having trouble deciding. I’ll make a post here when a decision has been reached.
Popularity: 44% [?]

Looks great! Have you considered Apache Tomahawk Sandbox as well?
December 8th, 2006 | #
I was going to suggest the Tomahawk Sandbox also.
December 8th, 2006 | #
I have considered the Tomahawk Sandbox, but decided against it for a number of reasons. One of the goals I hope to achieve with this donation/migration is to create a nice set of components to bundle with (and offer separate from) the 1.2 RI. While the impl association is a nice-to-have (for me), the JSF version is pretty important, as I’ve been using 1.2 APIs. To date, MyFaces has not released a 1.2 impl that I’m aware of, so Tomahawk is ruled out from the start. And, given the licensing of some of the code (CDDL and GPL), inclusion in Tomahawk would be diffficult, I would think, and I just don’t have time to chase that down.
With Ryan Lubke’s opening of the sandbox and Ed’s offer of inclusion there, one of these two solutions just seemed a better fit.
December 11th, 2006 | #
cool components. Are they dependent on apache.commons.fileupload?
Any news on the release?
December 14th, 2006 | #
Watch this space. Announcement pending.
December 14th, 2006 | #
I’m very interested in the “Multi-file Uploads”. Can you tell me where can I find to download it ?
December 19th, 2006 | #
Luis,
Sorry for the slow response. I’ve been on vacation over the Christmas holiday, so I’ve been mostly away from things. Right now, there’s nowhere to download a binary, though we’re working on getting a nightly build set up for that. I’ll try to build a binary and upload here for you in the next day or two.
December 31st, 2006 | #
Hello Jason,
I’m pretty much interested in your Download component. I checked it out from the sandbox and tried to use it (method=”download”). It generates the GetPdf link but then I got :
“Etat HTTP 404 - /secure/Sandbox___Download.jsp”
I saw a reference to /Sandbox___Download in FileDownload.java but I don’t understand where Sandbox___Download.jsp should come from.
Can you explain ?
Thanks
January 5th, 2007 | #
Well, that is a “fake” URL that should trigger the JSF lifecycle, allowing the PhaseListener to pick up the request. Are you using prefix mapping? I’m guessing that’s what the problem is. I haven’t done a good deal of testing using prefix mapping, as we always use suffix mapping (*.jsf). Let me take a look at that, and I’ll try to get a fix committed today.
January 5th, 2007 | #
Paul, I was almost right. It was in the right area of the code, but slightly different issue. You can check the CVS commit message if you’d like the details, but I should have that issue fixed for you. All you’ll need to do is update your tree and rebuild. I tested both prefix and suffix mapped just to make sure things are working, and, based on my simple test application, that appears to be the case. Let me know if you continue to have problems.
January 5th, 2007 | #
Thanks a lot Jason, your component is now working like a charm.
January 11th, 2007 | #
I downloaded your component but I’m getting some errors.. please I need a full example, including the beans for “download” and “multi-file-upload” tools.
February 12th, 2007 | #
Hi. I’m interested in trying out your download component. I’ve read in this page that some people have tried it, but I can’t find where to download it from.
Could someone please give me a hint? Thank you very much!!!
February 28th, 2007 | #
Adriana, you can find info about downloading and demoing the Sandbox components here.
February 28th, 2007 | #
Marceli, what errors are you getting? If you’d like to see a sample of using the download and mult-file upload components, check out the demo app (source code).
February 28th, 2007 | #
Thanks for the help!!!!:smile:
March 1st, 2007 | #
Jason, thanks for the answer. I’ve just tried to see the multi-file example at this link but if I click “Add Files” nothing happens (it’s supposed to open file chooser, right?).
I checked at firefox and IE.
Can you help me?
March 2nd, 2007 | #
Marceli, it looks like there’s a permissions problem on the applet. It should have been signed when the library was built, but it appears that did not happen for some reason. I’ll try to get that fixed.
March 5th, 2007 | #
Hi. I’m trying to use your download component in a .xhmtl page. The EL expression in the data tag is invoked correctly in my bean, but when I go back to the page, it doesn’t show me the link where I have to click to get the dialog box. I’ve tried putting an image instead of text, but that doesn’t work either. Any ideas of what I could be doing wrong?? Thanks!
March 6th, 2007 | #