0

我仍然是 Spring 和 Web 编程的初学者。

我正在尝试创建一个将 modelAttribute 发送到另一个页面但 modelAttribute 始终为空的小表单。

文件上传表单.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;
          }

   }

我的上传测试.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>

文件上传控制器.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 );                  
      } 
   }

我知道代码看起来不漂亮并且会抛出错误,但是我在 FileUploadController.java 文件中设置了一个断点来检查传递给它的参数中的内容,并且 mainForm 总是返回 null。

任何人都知道为什么它会为空,而我知道我的 inputtext id="blabla" 确实有文本。

谢谢你的帮助

4

2 回答 2

0

通过 GET 加载带有表单的页面时,您是否将 Form-Object 添加到模型中?也许这样的事情可以解决问题:

@RequestMapping( value = "/MyuploadTest", method = RequestMethod.GET) 
public String myUploadTest(Model model) {        
    model.addAttribute("fileUploadform", new FileUploadForm());
    return "MyuploadTest";                  
} 
于 2013-12-06T18:42:24.483 回答
0

最可能的原因是您没有包含form:input相反,您使用的是input ,如果您有form:form 标签,它将不起作用。您也忘记了 path="modelAttributeName"。

于 2019-01-04T21:27:15.260 回答