2011-03-10 74 views
1

我有一個表單,其中包含字段和文件上傳元素,同時提交表單時,它正在拋出一個空指針異常,當我登錄表單對象時,所有字段變爲空,當我刪除表格enctype="multipart/form-data"1然後我得到所有字段,但文件對象變爲空。隨着字段和文件上傳

表格代號:

<form:form method="post" id="form" name="frm" action="${action}" enctype="multipart/form-data"> 
    <table> 
     <tr> 
      <td><form:label path="productName">Product Name: </form:label></td> 
      <td> 
       <form:input path="productName"/> 
      </td> 
     </tr>   

     <tr> 
      <td><form:label path="rfile">Receipt File</form:label></td> 
      <td><form:input path="rfile" id="receiptFile" type="file" /></td> 
     </tr>   
     <tr> 
      <td colspan="2"><input type="submit" value="Upload Receipt" /></td> 
     </tr> 
    </table>  
</form:form> 

控制器代碼:

@RequestMapping(value="/test.do", method = RequestMethod.POST) 
public ModelAndView testReceipt(@ModelAttribute("frm") ReceiptForm form, BindingResult result, HttpServletRequest request){ 

    System.out.println("---"+form.getProductName()); 
    System.out.println("---"+form.getRfile()); 
} 

請注意文件是一個類型:CommonsMultipartFile

+0

示例代碼將非常有幫助。 – 2011-03-10 10:16:00

回答