Search code examples
jspstripes

Uploading multiple files with stripes


I am trying to upload multiple files and bind them to a java array in the stripes framework. I have read the documentation here and this question on SO. However, I am still having issues. While running in debug, if I upload multiple files I notice that only the last file is bound to the array. What am I doing incorrectly?

<stripes:form>
<c:forEach varStatus="loop" begin="0" end="3">
<stripes:file name="attachments[${loop.index}]"/>
</c:forEach>
<stripes:submit name="submit" />  
</stripes:form>

private List<FileBean> attachments = new ArrayList<FileBean>();
public void setAttachments(List<FileBean> attachments) throws IOException {
    logger.info("*********************Attachments " + attachments.size());
    this.attachments = attachments;
    //documentation says to call FileBean.save or read them as a stream

}

Solution

  • You can leave out the setter. The attachments variable is already initialized via the new operator.

    private List<FileBean> attachments = new ArrayList<FileBean>();
    
    public List<FileBean> getAttachments() {
        return this.attachments;
    }
    
    public Resolution submit() {
        System.out.println("********************* Attachments " + attachments.size());
        return show();
    }
    
    @DefaultHandler
    public Resolution show() {
        return new ForwardResolution("[path to jsp]");
    }
    

    Pressing 'submit' on the form calls the 'submit' Resolution where you can now inspect the populated attachments, save them, and - in this example - return to the form again. Or you could show your visitors another page.