How to Customize the Toolbar

Using Input Components and Defining Forms 9-37 cache UploadedFile objects across requests. If you need to keep the file, you must copy it into persistent storage before the request finishes. For example, instead of storing the file, add a message stating the file upload was successful using a managed bean as a response to the ValueChangeEvent event, as shown in Example 9–10 . Example 9–10 Using valueChangeListener to Display Upload Message JSF Page Code ----- af:form usesUpload=true af:inputFile label=Upload: valueChangeListener={managedBean.fileUploaded} af:commandButton text=Begin af:form Managed Bean Code ---- import javax.faces.application.FacesMessage; import javax.faces.context.FacesContext; import javax.faces.event.ValueChangeEvent; import org.apache.myfaces.trinidad.model.UploadedFile; public class ABackingBean { ... public void fileUploadedValueChangeEvent event { UploadedFile file = UploadedFile event.getNewValue; if file = null { FacesContext context = FacesContext.getCurrentInstance; FacesMessage message = new FacesMessage Successfully uploaded file + file.getFilename + + file.getLength + bytes; context.addMessageevent.getComponent.getClientIdcontext, message; Heres where we could call file.getInputStream } } } You can also handle the upload by binding the value directly to a managed bean, as shown in Example 9–11 . Example 9–11 Binding the Value to a Managed Bean JSF Page Code ---- af:form usesUpload=true af:inputFile label=Upload: value={managedBean.file} af:commandButton text=Begin action={managedBean.doUpload} af:form Managed Bean Code ---- import org.apache.myfaces.trinidad.model.UploadedFile; public class AManagedBean { public UploadedFile getFile { return _file; } 9-38 Web User Interface Developers Guide for Oracle Application Development Framework public void setFileUploadedFile file { _file = file; } public String doUpload { UploadedFile file = getFile; ... and process it in some way } private UploadedFile _file; }

9.9.1 How to Use the inputFile Component

A Java class must be bound to the inputFile component. This class will be responsible for containing the value of the uploaded file. To add an inputFile component: 1. Create a Java class that will hold the value of the input file. It must be an instance of the org.apache.myfaces.trinidad.model.UploadedFile interface. 2. In the Component Palette, from the Common Components panel, drag and drop an Input File onto the page.

3. Set value to be the class created in Step 1.

4. If you want the value of the component to appear as read-only until the user hovers over it, expand the Other section and set Editable to onAccess. If you want the component to always appear editable, select always. If you want the value to be inherited from an ancestor component, select inherit.

5. From the Component Palette, drag and drop any command component onto the

page. This will be used to initiate the upload process.

6. With the command component selected, set the actionListener attribute to a

listener that will process the file after it has been uploaded.

9.9.2 What You May Need to Know About Temporary File Storage

Because ADF Faces will temporarily store files being uploaded either on disk or in memory, by default it limits the size of acceptable incoming upload requests to avoid denial-of-service attacks that might attempt to fill a hard drive or flood memory with uploaded files. By default, only the first 100 kilobytes in any one request will be stored in memory. Once that has been filled, disk space will be used. Again, by default, that is limited to 2,000 kilobytes of disk storage for any one request for all files combined. Once these limits are exceeded, the filter will throw an EOFException. Files are, by default, stored in the temporary directory used by the java.io.File.createTempFile method, which is usually defined by the system property java.io.tmpdir. Obviously, this will be insufficient for some Note: If you select inherit, and no ancestor components define the editable value, then the value always is used.