Last updated December 19, 2008 20:06, by Jason Lee

<sc:download> - An Easy Way to Deliver Dynamic Content

The <sc:download> component offers a way to download or embed an arbitrary binary object, whether it's a dynamically generated image you would like to display on your web page, or a PDF-based report you'd like to download to your user. There are a number of ways to achieve this, from Servlets to custom PhaseListeners. The problem with the Servlet approach is that it lives outside the JSF lifecycle, so there's a fair amount of work for the Servlet to do to force itself in. The PhaseListener approach, however, does live inside the JSF lifecycle, so it has immediate access to all that it needs. In fact, that's the approach download takes. The downside to the PhaseListener is the complexity involved in configuring the PhaseListener, managing the URI that triggers its code, and acquiring the resource requested.

The <sc:download> component, however, hides all of the from the page author. Take, for example, the use case in which you would like to load an image from the Classpath (or generate it on the fly), and display that to the user. You're code might look something like this:

To make use of that method from your web page, you would need something like this:

Note that the display of the image is actually done via the <h:graphicImage> component. The <sc:download> components defines, for any child components, an EL value which they can use to get the URL associated with the given resource. The default variable name, downloadUrl, can be changed if that happens to conflict with an variable already defined, as might happen inside a <h:dataTable>. To do so, simply provide the urlVar attribute:

If you would like to download the object to the user, you might try something like this:

Note the method. Here we specify that we want to download the object. We then specify the mime type and filename to use for the download. Note, also, the nested <h:graphicImage> component. This will be rendered as an anchor for the user to click to initiate the download. When clicked, the user will be asked to save the PDF names sample.pdf. It's as simple as that!

  • Mysql
  • Glassfish
  • Jruby
  • Rails
  • Nblogo
Terms of Use; Privacy Policy;
© 2014, Oracle Corporation and/or its affiliates
(revision 20160708.bf2ac18)
Please Confirm