Spring Form post does not send ModelAttribute to controller

3.6k views Asked by At

I'm still a beginner user with spring and web programming.

I'm trying to create a small form that would send a modelAttribute to another page but the modelAttribute is always null.

FileUploadForm.java

    public class FileUploadForm {

           CommonsMultipartFile file;   String blabla;

           // initiation of FileUploadForm.

           public FileUploadForm() {        
              // TODO Auto-generated constructor stub       
              file= null;   
           }        
           public String getBlabla() {      
             return blabla;
           }

           public void setBlabla( String blabla ) {         
               this.blabla = blabla;    
           }

          public CommonsMultipartFile getFile() {       
              return file;  
          }

          public void setFile(CommonsMultipartFile file) {      
             this.file = file;
          }

   }

MyuploadTest.jsp

<script>    
    function addFileToEcc() {
            document.getElementById('file_upload_form').target = 'upload_target';
    };  

</script>
<div>
    <div id="titleSection" style="padding:5px;color:#303030;"class="ui-state-active, ui-widget-content ui-state-active, ui-widget-header ui-state-active"  onclick="$('#fourthSection').slideToggle();" >
        <label>detail</label>
    </div>

    <div id='AttachSection'>
        <form:form id="file_upload_form" action="NewfileUpload.do" method="post" modelAttribute="fileUploadform" enctype="multipart/form-data">
            <input type="text" id='blabla' name="blabla" value="boubou" />
            <iframe id="upload_target" name="upload_target" src=""></iframe>                        
        </form:form>
    </div>
</div>

FileUploadController.java

@Controller     
    @SessionAttributes  
    public class FileUpload extends ServiceImpl {

        @Autowired
        Log log;        
        @Autowired 
        FileManagementService gFM;

    @RequestMapping( value = "/NewfileUpload.do", method = RequestMethod.POST ) 
    //      public ModelAndView uploadFile( @ModelAttribute FileUploadForm mainForm, HttpServletRequest pRequest ) {        
       public ModelAndView uploadFile( @ModelAttribute("fileUploadform") FileUploadForm mainForm, HttpServletRequest pRequest ) {
             log.info( "Begin File Upload." );
             FileUploadDetails fud = new FileUploadDetails();                            
             fud.setStatusCode( FileUploadDetails.STATUS_SUCCESS );

             return new ModelAndView( "fileUploaded", "fud", fud );                  
      } 
   }

I know that the code doesn't look pretty and would throw errors the way is is but I have setup a breakpoint in the FileUploadController.java file to check what are in the parameter passed to it and mainForm always returns null.

Anyone has an idea why it would be null while i know that my inputtext id="blabla" does have text in it.

THanks for you help

2

There are 2 answers

14
Jonas On

Are you adding the Form-Object to the model when loading the page with your form via GET? Maybe something like this solves the problem:

@RequestMapping( value = "/MyuploadTest", method = RequestMethod.GET) 
public String myUploadTest(Model model) {        
    model.addAttribute("fileUploadform", new FileUploadForm());
    return "MyuploadTest";                  
} 
0
Kurosh Farsimadan On

The most probable reason is that you are not including the form:input Instead, you are using input, which will not work if you have the form:form tag. You are also forgetting the path="modelAttributeName".