Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Adding a File Upload to a Form

Stripes makes it very easy to manage file uploads. At its simplest you just include a file field in a stripes form, and match it with a FileBean property on your ActionBean. For example:

Code Block
xml
languagexml
titleUsing a file field on a JSP
<stripes:form> 
    ... 
    <stripes:file name="newAttachment"/> 
    ... 
</stripes:form> 
Code Block
languagejava
titleA FileBean property in an ActionBean
private FileBean newAttachment; 

public FileBean getNewAttachment() { 
    return newAttachment; 
} 

public void setNewAttachment(FileBean newAttachment) { 
    this.newAttachment = newAttachment; 
} 

...

The following code shows how you might refactor the code from above to accept multiple files:

Code Block
xml
languagexml
titleUsing an indexed file field on a JSP
<stripes:form> 
    <c:forEach ... varStatus="loop"> 
    ... 
    <stripes:file name="newAttachments[${loop.index}]"/> 
    ... 
</stripes:form> 
Code Block
languagejava
titleAn Indexed FileBean property in an ActionBean
private List<FileBean> newAttachments; 

public List<FileBean> getNewAttachments() { 
    return this.newAttachments; 
} 

public void setNewAttachment(List<FileBean> newAttachments) { 
    this.newAttachments = newAttachments; 
} 

...

In case you're going to use CommonsFileupload and also have the cos library in your classpath, you can explicitly configure the CommonsMultipartWrapper by adding the following initialization parameter to the Stripes Filter:

Code Block
xml
languagexml
<init-param> 
    <param-name>MultipartWrapper.Class</param-name> 
    <param-value>net.sourceforge.stripes.controller.multipart.CommonsMultipartWrapper</param-value> 
</init-param> 

It is also possible to plug-in replace the code that is responsible for manufacturing an instance of MultipartWrapper in order to add additional control behaviour. This can be done by implementing MultipartWrapperFactory and specifying your custom factory as an initialization parameter to the Stripes Filter.

Code Block
xml
languagexml
<init-param> 
    <param-name>MultipartWrapperFactory.Class</param-name> 
    <param-value>com.myco.CustomMultipartWrapperFactory</param-value> 
</init-param>