Upload Files with JSF and MyFaces

Upload Files with JSF and MyFaces

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:

The output produced by the result page
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.

Return to ONJava.com.

Prev  [1] [2] [3] [4] [5] [6] 

Close    To Top
  • Prev Article-Java:
  • Next Article-Java:
  • Now: Tutorial for Web and Software Design > Java > Java Servlets > Java Content
    Photoshop Tutorial
     

    Special Effect

      3D Effect
      Photoshop Articles
    Programming Tutorial
     

    C/C++ Tutorial

      Visual Basic
      C# Tutorial
    Database Tutorial
     

    MySQL Tutorial

      MS SQL Tutorial
      Oracle Tutorial
    Geek Tutorial
     

    Blogging Tutorial

      RSS Tutorial
      Podcasting Tutorial
    Graphic Design Tutorial
      Coreldraw Tutorial
      Illustrator Tutorial
      3D Tutorials
    Webmaster Articles
     

    Domain Service

      Web Hosting
      Site Promotion
    Java Tutorial/ Articles
     

    Java Servlets

      JavaEE Tutorial
     

    JavaBeans Tutorial

    XML Tutorial/ Articles
     

    XML Style

      AJAX Tutorial
      XML Mobile
    Flash Tutorial/ Articles
     

    Flash Video

      Action Script
      Flash Articles
    OS Tutorial/ Articles
      Linux Tutorial
      Symbian Tutorial
      MacOS Tutorial
    Personal Tech
      Hardware Tutorial
      Software Tutorial
      Online Auction