Given a struts action form that contains file upload field and server side validation is turned on. If the form does not pass the validation test and is returned back to the inputting jsp page, the upload file field will no longer contain the value that the user had inputted. This can be frustrating for the user. (In my situation, I had a few file upload fields and user had to re-enter all of them if the form does not pass the validation test, even if the error is not on the upload fields)
After searching on the web, I found that the upload field itself is does not take any default value. And because of security reason, values can not be force into the upload field by javascript.
Workaround:
In my action form, I added another FormFile variable and a String for storing the filename.
protected FormFile uploadFile;
protected FormFile uploadFilePrev;
protected String uploadFilenamePrev;
public void setUploadFile (FormFile uploadFile) {
this. uploadFile = uploadFile;
if (uploadFile.getFileSize() > 0) {
this. uploadFile Prev = uploadFile;
}
if (this.uploadFilePrev != null) {
this. uploadFilenamePrev = this. uploadFilePrev.getFileName();
}
}
In the jsp:
<c:out value="${myActionForm. uploadFilenamePrev }" />
<html:file property=" uploadFile "/>
With this, the file previously selected is saved and the filename can be displayed in the jsp. Note that you need to account for the previously selected file in your validation code.