我有一个包含字段和文件上传元素的表单,同时提交表单时抛出空指针异常,当我记录表单对象时,所有字段都变为空,当我删除表单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>
COntroller代码:
@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