I think I have seen every post on the web about this topic but I cannot correct that bug :(
I have a Web App using Spring Security and Spring Mvc and I want to create a form to upload an image (you have to be logged to do that) but whatever the way I twist my code with what I find on forums, I have an Error 405 Request method 'POST' not supported when uploading a file
Here is my applicationContext.xml :
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.meltdown.*" />
<context:annotation-config />
<bean id="userDAO" class="com.meltdown.bo.users.infra.impl.JdbcUserDAO">
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="userService" class="com.meltdown.bo.users.application.service.impl.StandardUserService" />
<bean id="barDAO" class="com.meltdown.bo.bars.infra.impl.JdbcBarDAO">
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="barService" class="com.meltdown.bo.bars.application.service.impl.StandardBarService" />
<bean id="newsDAO" class="com.meltdown.bo.news.infra.impl.JdbcNewsDAO">
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="newsService" class="com.meltdown.bo.news.application.service.impl.StandardNewsService" />
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- one of the properties available; the maximum file size in bytes -->
<property name="maxUploadSize" value="100000"/>
</bean>
</beans>
my Controller :
@Controller
public class FileUploadController {
@RequestMapping(value="/bo/uploadImage", method = RequestMethod.GET)
public String uploadImage() {
return "bo_uploadimage";
}
@RequestMapping(value="/bo/uploadImage", method = RequestMethod.POST)
public String uploadImage(@RequestParam(value = "file")FileUploadBean file, BindException errors, Map<String, Object> model) {
System.out.println("#############################" + file);
return "bo_uploadimage";
}
}
public class FileUploadBean{
private byte[] file;
public void setFile(byte[] file) {
this.file = file;
}
public byte[] getFile() {
return file;
}
}
jsp:
<html>
<head>
<title>Upload a file please</title>
</head>
<body>
<h1>Please upload a file</h1>
<form method="post" action="/meltdown/bo/uploadImage" enctype="multipart/form-data">
<input type="file" name="file"/>
<input type="submit"/>
</form>
</body>
</html>
I think the problem come from my controler and maybe because I mixed up Spring 4 annotations with Spring3 conf?
Thank's for your help!!
Edit mvc-dispatcher-servlet.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<context:component-scan base-package="com.meltdown.*" />
<mvc:annotation-driven />
<bean id="messageSource"
class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="classpath:messages" />
<property name="defaultEncoding" value="ISO-8859-1" />
</bean>
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/pages/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- one of the properties available; the maximum file size in bytes -->
<property name="maxUploadSize" value="100000"/>
</bean>
</beans>