SpringMVC出現no mapping found for http request的解決辦法
錯誤一:
檢查web.xml中的<servlet-mapping></servlet-mapping>
的<url-pattern></url-pattern>
將
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
改爲:
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
錯誤二:
檢查springmvc的配置文件(這裏我的文件名爲springmvc.xml):
這裏有兩點需要注意:
- 必須開啓註解
<mvc:annotation-driven />
<context:component-scan base-package="com.xrda.controller"></context:component-scan>
中的base-package需要是包級別的。 下邊是我的項目中的包
錯誤三:
還有一個確定的因素在這裏也貼出來吧,如果以上兩種方法都不行的話可以試試:
在springmvc配置文件中配置
<mvc:default-servlet-handler/>
跟錯誤二是同一個文件
錯誤四:
這個錯誤是比較好發現的,就是映射的地址寫錯了,比如:
@Controller
@RequestMapping(value = "/test")
public class FileUploadController {
@RequestMapping(value = "/upload.htm" , method = RequestMethod.POST)
public String upload(MultipartFile uploadFile,HttpSession session) throws IllegalStateException, IOException{
String fileName = uploadFile.getOriginalFilename();
String filePath = session.getServletContext().getRealPath("upload");
File file = new File(filePath, fileName);
uploadFile.transferTo(file);
System.out.println(filePath);
return "message";
}
}
我們訪問的時候的地址應該是http://localhost:8080/項目名/test/upload.htm
菜鳥一枚,大神勿噴。