Announcement Announcement Module
Collapse
No announcement yet.
File upload - formBackingObject called before onSubmit! Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • File upload - formBackingObject called before onSubmit!

    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:

    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&#58;if test="$&#123;photoInfo.numPhotos > 0&#125;">             
    			<td> Replace&#58;  <select name="replace">
    				<c&#58;if test="$&#123;!&#40;photoInfo.numPhotos == 3&#41;&#125;">
                    	<option value="0">Don't replace</option> 
                    </c&#58;if>
                    <c&#58;forEach items="$&#123;photoInfo.photoList&#125;"  var="photo" varStatus="varStatus">              
                    	<option  value="<c&#58;out value="$&#123;photo.photoID&#125;"/>" >$&#123;varStatus.index + 1&#125;</option> 
                    </c&#58;forEach>                  
                    </select>
                </td>
                </c&#58;if>
              </tr>
            </table>
            <p> 
              <input type="submit"  value="Upload Photo">
          </form>
          <c&#58;if test="$&#123;photoInfo.numPhotos > 0&#125;">    
          <table width="100%" border="1" cellpadding="5" cellspacing="5">
          	<tr> 
          	<c&#58;forEach items="$&#123;photoInfo.photoList&#125;"  var="photo" varStatus="varStatus"> 
          		<td> 
                <p><img src="<c&#58;out value="$&#123;photo.filePath&#125;"/>" width="<c&#58;out value="$&#123;photo.imageWidth&#125;"/>" height="<c&#58;out value="$&#123;photo.imageHeight&#125;"/>"> 
                </p>
                <p>Photo <c&#58;out value="$&#123;varStatus.index + 1&#125;"/></p>
              	</td> 
         	</c&#58;forEach>  
            </tr>
          </table>        
    	  </c&#58;if>
    My controller:

    Code:
    public class PhotoUploadController extends SimpleFormController&#123;
    
          private PhotoHandler photoHandler;
    
        protected ModelAndView onSubmit&#40;HttpServletRequest request,
                HttpServletResponse response, Object command, BindException errors&#41;
                throws ServletException, IOException &#123;
    
            // cast the bean
            PhotoInfo photoInfo = &#40;PhotoInfo&#41; command;
            String userName = &#40;String&#41; request.getSession&#40;false&#41;.getAttribute&#40;
                    "userName"&#41;;
        
            int photoIDOfPhotoToReplace = RequestUtils.getIntParameter&#40;request,
            "replace", 0&#41;;
            String fileName = photoInfo.getFile&#40;&#41;.getOriginalFilename&#40;&#41;;
            byte&#91;&#93; file = photoInfo.getFile&#40;&#41;.getBytes&#40;&#41;;
            String newFileName = new String&#40;PhotoHandler.getTempImageDirectory&#40;&#41;+ "/"
                    + userName + "_" + fileName&#41;;
            BufferedOutputStream bos = new BufferedOutputStream&#40;
                    new FileOutputStream&#40;newFileName&#41;&#41;;
            bos.write&#40;file&#41;;
            bos.flush&#40;&#41;;
            bos.close&#40;&#41;;
            boolean fileHandledOK = photoHandler.handleNewFile&#40;userName,
                    newFileName, photoIDOfPhotoToReplace&#41;;
            if &#40;fileHandledOK&#41;
                return new ModelAndView&#40;getSuccessView&#40;&#41;&#41;;
            else &#123;
                photoInfo.setActionMessage&#40;"Sorry, the upload failed, please try again."&#41;;
                return new ModelAndView&#40;getFormView&#40;&#41;&#41;;
            &#125;
        &#125;
    
        /*
         * &#40;non-Javadoc&#41;
         * 
         * @see org.springframework.web.servlet.mvc.AbstractFormController#formBackingObject&#40;javax.servlet.http.HttpServletRequest&#41;
         */
        protected Object formBackingObject&#40;HttpServletRequest request&#41;
                throws Exception &#123;
            String userName = &#40;String&#41; request.getSession&#40;false&#41;.getAttribute&#40;
                    "userName"&#41;;
            PhotoInfo photoInfo = new PhotoInfo&#40;&#41;;
            List photoList = photoHandler.getPhotosDAO&#40;&#41;.getAllPhotos&#40;userName&#41;;
            photoInfo.setPhotoList&#40;photoList&#41;;
            return photoInfo;
        &#125;
    ...
    Can anyone suggest what might be causing this strange behaviour?

    Thanks,

    John Pedersen

  • #2
    Take a look at the workflow for AbstractFormController and SimpleFormController

    Comment


    • #3
      Thanks for the hint - don't know how I overlooked that.

      John

      Comment

      Working...
      X