Hi,
I am uploading image files in my webapp, but the images are not being displayed until I refresh the page. Looking at the debugger, I see that when the submit button is hit, formBackingObject in my controller is being called, then onSubmit.
Here's my JSP including the upload form:
My controller:Code:<form name="form1" enctype="multipart/form-data" method="post" action="" > <p></p> <table width="75%" border="1"> <tr> <td><input name="file" type="file"> </td> <c:if test="${photoInfo.numPhotos > 0}"> <td> Replace: <select name="replace"> <c:if test="${!(photoInfo.numPhotos == 3)}"> <option value="0">Don't replace</option> </c:if> <c:forEach items="${photoInfo.photoList}" var="photo" varStatus="varStatus"> <option value="<c:out value="${photo.photoID}"/>" >${varStatus.index + 1}</option> </c:forEach> </select> </td> </c:if> </tr> </table> <p> <input type="submit" value="Upload Photo"> </form> <c:if test="${photoInfo.numPhotos > 0}"> <table width="100%" border="1" cellpadding="5" cellspacing="5"> <tr> <c:forEach items="${photoInfo.photoList}" var="photo" varStatus="varStatus"> <td> <p><img src="<c:out value="${photo.filePath}"/>" width="<c:out value="${photo.imageWidth}"/>" height="<c:out value="${photo.imageHeight}"/>"> </p> <p>Photo <c:out value="${varStatus.index + 1}"/></p> </td> </c:forEach> </tr> </table> </c:if>
Can anyone suggest what might be causing this strange behaviour?Code:public class PhotoUploadController extends SimpleFormController{ private PhotoHandler photoHandler; protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object command, BindException errors) throws ServletException, IOException { // cast the bean PhotoInfo photoInfo = (PhotoInfo) command; String userName = (String) request.getSession(false).getAttribute( "userName"); int photoIDOfPhotoToReplace = RequestUtils.getIntParameter(request, "replace", 0); String fileName = photoInfo.getFile().getOriginalFilename(); byte[] file = photoInfo.getFile().getBytes(); String newFileName = new String(PhotoHandler.getTempImageDirectory()+ "/" + userName + "_" + fileName); BufferedOutputStream bos = new BufferedOutputStream( new FileOutputStream(newFileName)); bos.write(file); bos.flush(); bos.close(); boolean fileHandledOK = photoHandler.handleNewFile(userName, newFileName, photoIDOfPhotoToReplace); if (fileHandledOK) return new ModelAndView(getSuccessView()); else { photoInfo.setActionMessage("Sorry, the upload failed, please try again."); return new ModelAndView(getFormView()); } } /* * (non-Javadoc) * * @see org.springframework.web.servlet.mvc.AbstractFormController#formBackingObject(javax.servlet.http.HttpServletRequest) */ protected Object formBackingObject(HttpServletRequest request) throws Exception { String userName = (String) request.getSession(false).getAttribute( "userName"); PhotoInfo photoInfo = new PhotoInfo(); List photoList = photoHandler.getPhotosDAO().getAllPhotos(userName); photoInfo.setPhotoList(photoList); return photoInfo; } ...
Thanks,
John Pedersen


Reply With Quote