SpringMVC Bug(一)
環(huán)境:springmvc-4.x
? tomcat7.9
? idea 2017
? jdk 1.8
錯誤提示:Required request part 'file' is not present
- 遇到這種問題一般情況下都是參數(shù)未對應上,但是在我本次處的問題卻并不是這么一回事
前端代碼
<form action="testFileUpload" method="post" enctype="multipart/form-data">
file:<input type="file" name="file">
desc:<input type="text" name="desc">
<input type="submit" value="submit">
</form>
配置代碼
<bean class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="UTF-8"></property>
<property name="maxUploadSize" value="10240000"></property>
</bean>
在這一步不知道大家有木有發(fā)現(xiàn)問題:
接下來看后端代碼
public String testFileUpload(@RequestParam(value = "file") MultipartFile file,@RequestParam(value = "desc") String desc)
{
System.out.println("desc"+desc);
System.out.println(file.getOriginalFilename());
return "success";
}
這就是全部邏輯代碼,運行出的錯就是
Required request part 'file' is not present
在一番Debug之后找到了錯誤原因,是配置文件出了問題:
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="UTF-8"></property>
<property name="maxUploadSize" value="10240000"></property>
</bean>
這樣之后就解決了問題,
但是這是為什么呢?

1550908155055.png
因為dispatcher會自行調用名為multipartResolver的實例,如果沒有加iddispatcher就不知道該調用誰了。