Downloading with PrimeFaces 2.0

Since this is our first recipe based on PrimeFaces, let's say that the definition of PrimeFaces is:

Its an open source component suite for Java Server Faces featuring 70+ Ajax powered rich set of JSF components. Additional TouchFaces module features a UI kit for developing mobile web applications.

The main features of PrimeFaces are as follows (more details at the PrimeFaces home page http://www.primefaces.org/ and show case page— http://www.primefaces.org:8080/prime-showcase/ui/home.jsf):

  • Rich set of components (HtmlEditor, Dialog, AutoComplete, Charts, and many more)
  • Built-in Ajax with Lightweight Partial Page Rendering
  • Native Ajax Push/Comet support
  • Mobile UI kit to create mobile web applications for handheld devices with webkit-based browsers (iPhone, Palm, Android Phones, Nokia S60, and more)
  • Compatible with other component libraries
  • Unobstrusive JavaScript
  • Extensive documentation

Now, in this recipe you will see how to use the fileDownload component of PrimeFaces 2.0.

Getting ready

We developed this recipe with NetBeans 6.8, JSF 2.0, and GlassFish v3. The JSF 2.0 classes were obtained from the NetBeans JSF 2.0 bundled library. In addition, we have used PrimeFaces 2.0, which provide support for JSF 2.0. You can download this distribution from http://www.primefaces.org/. The PrimeFaces libraries (including necessary dependencies) are in the book code bundle, under the /JSF_libs/PrimeFaces JSF 2.0 folder.

How to do it...

The PrimeFaces fileDownload component is very easy to use. Practically, we need to write a JSF page and a managed bean to obtain the desired result. The JSF page exploit the fileDownload component as shown next:

…
<h:form>
<h:outputText value="Download our file:"/>
<p:commandButton value="Download" async="false">
<p:fileDownload value="#{downloadBean.file}" />
</p:commandButton>
</h:form>
…

Note

The p:commandButton component is a PrimeFaces component that extends the standard h:commandButton with AJAX, partial processing, and confirmation features. In older versions of PrimeFaces the async attribute of this component is known as ajax. Its value is still a Boolean, and it tells JSF whether the action is AJAX-ified or not. You can use an h:commandButton instead of the p:commandButton with no problem. The complete PrimeFaces tags reference can be found at http://primefaces.prime.com.tr/docs/tag/.

Next, the DownloadBean (see the reference to it in the value attribute of the p:fileDownload component), provides access to the downloadable resources through a StreamedContent, which is a PrimeFaces class used to stream dynamic contents like inputstream to the client. The source of our bean is listed next:

public class DownloadBean {
private StreamedContent file;
public DownloadBean() {
ExternalContext extContext =
FacesContext.getCurrentInstance().getExternalContext();
try {
file = new DefaultStreamedContent(new FileInputStream(
extContext.getRealPath("/download/primefaces.pdf")),
"application/pdf", "primefaces.pdf");
} catch (IOException e) {
e.printStackTrace();
}
}
public StreamedContent getFile() {
return file;
}
public void setFile(StreamedContent file) {
this.file = file;
}

The PrimeFaces page dedicated to this component can be accessed at http://www.primefaces.org:8080/prime-showcase/ui/fileDownload.jsf.

How it works...

When the user initiates the download action (by pressing a button, a link, and so on) the PrimeFaces StreamedContent class accesses the resource to be downloaded. Actually, the StreamedContent is an interface implemented by the DefaultStreamedContent class, which is obviously the default implementation of this interface. More details about this class are available at http://primefaces.prime.com.tr/docs/api/.

See also

The code bundled with this book contains a complete example of this recipe. The project can be opened with NetBeans 6.8 and it is named: Download_with_PrimeFaces_2_0.

Personally, I recommend you to check the PrimeFaces Show Case page at http://www.primefaces.org:8080/prime-showcase/ui/home.jsf, if you want to see some great components ready to be used with JSF 2.0.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset