0

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

user2720864
  • 8,015
  • 5
  • 48
  • 60
Yan
  • 11
  • 1
  • 2
  • Please format your code. The method argument is wrongly named. You have an attribute called `fileUploadform` not `mainForm`. Either rename it in the JSP or in your controller. Also your controller looks suspicious why would a controller extend a `ServiceImpl`. Your form object shouldn't rely on `CommonsMultiPartFile` but on the common interface `MultiPartFile`. – M. Deinum Dec 06 '13 at 17:33

2 Answers2

0

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";                  
} 
Jonas
  • 1,315
  • 1
  • 18
  • 31
  • You are right Jeyp I did had that controller too which i forgot to add that part that was on my code as well. @RequestMapping( value = "/uploadTest.do", method = RequestMethod.GET ) public String uploadScreen( Model model) { model.addAttribute("fileUploadform", new FileUploadForm()); System.out.println("uploadTest"); return "MyuploadTest"; } – Yan Dec 06 '13 at 18:57
  • sorry about the formatting not sure how to make it look formatted – Yan Dec 06 '13 at 19:00
  • Did you already change the other things that were mentioned by @M.Deinum above? If this does not help then also try this: Use `commandName="fileUploadform"` instead of `modelAttribute="fileUploadform"` which automatically sets the according id and name values. – Jonas Dec 06 '13 at 19:05
  • This is what i don't understand I know its going to the **FileUploadController** and my understanding that that spring should knows how to set the modelAttribute on post. but for reason the parameter *mainForm* in *FileUploadController* has null data in it – Yan Dec 06 '13 at 19:06
  • It's because it is mapped to the name of the form id or name (I don't know atm). Anyways, you specify id as "file_upload_form" in your jsp and dont specifically specify a name, so nothing matches with `@ModelAttribute("fileUploadform")` in your Controller. You should go with `commandName="fileUploadform"` and leave id/name/modelAttribute values blank – Jonas Dec 06 '13 at 19:14
  • M. Deinum, I changed the attribute to "fileUploadform". I'm not sure about using the interface MultiPartFile instead of CommonsultiPartFile, the thing is that i base my code on a colleague project that working, the person has left the company tho so i can't ask why he used CommonsMultiPartFile. – Yan Dec 06 '13 at 19:14
  • I removed the ids and put the commandName="fileUploadForm" `
    ` but still no luck. the attribute fileUploadform is still empty :(
    – Yan Dec 06 '13 at 19:30
  • Also try to use form:input as this adds binding to the command name. Using form:input, again, just specify path="blabla" and omit id/name. Is the form empty or is it null? Thats a difference. – Jonas Dec 06 '13 at 19:55
  • Jeyp, when i debug with a checkpoint on the parameter "fileUploadForm" in the the line "log.info("begin file upload") in the FileUploadController the parameter "FileUploadForm" has 2 properties "file" and "blablabla" both of them are null. ok now i'm going somewhere, with the path i can debug and i load the page MyuploadTest.jsp it run through getBlabla() but when i submit it doesn't go and set the variable. – Yan Dec 06 '13 at 22:13
  • Can you update your question, so it matches the latest changes in your code? It's hard to put it all togheter, as there have been many small changes yet. – Jonas Dec 07 '13 at 08:48
  • **MyUploadTest.jsp**
    – Yan Dec 11 '13 at 02:22
  • **FileUpload.java** `@Controller @SessionAttributes public class FileUpload { @RequestMapping( value = "/NewfileUpload.do", method = RequestMethod.POST ) public ModelAndView uploadFile( @ModelAttribute("fileUploadform") FileUploadForm pFile, HttpServletRequest pRequest ) { System.out.println( "Begin File Upload." ); System.out.println(pFile.getBlabla()); return new ModelAndView("FileUploadForm", "command", new FileUploadForm()); } }` – Yan Dec 11 '13 at 02:23
  • **MyFileUploadTestController** `@Controller @SessionAttributes( "FileUploadForm" ) public class MyuploadTestController { // @ModelAttribute("FileUploadForm") @RequestMapping( value = "/uploadTest.do", method = RequestMethod.GET ) public String uploadScreen( Model model, HttpServletRequest pRequest ) { model.addAttribute("fileUploadform", new FileUploadForm()); System.out.println("uploadTest"); return "MyuploadTest"; } }` – Yan Dec 11 '13 at 02:24
  • yet i still have the same issue getBlabla() return null while it has boubou in the textbox – Yan Dec 11 '13 at 02:26
0

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".

Eric Aya
  • 69,473
  • 35
  • 181
  • 253