I have following code:
@Controller
public class FileUploadController {
@Autowired
private AttachmentsToSendJDBCTemplate attachmentsToSendJDBCTemplate;
@RequestMapping(value = "/upload", method = RequestMethod.GET)
public String showUploadForm(HttpServletRequest request) {
return "upload";
}
@RequestMapping(value = "/doUpload", method = RequestMethod.POST)
public String handleFileUpload(HttpServletRequest request,
@RequestParam CommonsMultipartFile[] fileUpload) throws Exception {
int a = 5; //breakpoint is here;
upload.jsp:
<form method="post" action="doUpload" enctype="multipart/form-data">
<table border="0">
<tr>
<td><input type="file" name="fileUpload" size="50" /></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="Upload" /></td>
</tr>
</table>
</form>
web.xml:
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/application-context.xml,
/WEB-INF/spring-database.xml,
/WEB-INF/spring-security.xml,
/WEB-INF/spring-web-config.xml,
/WEB-INF/mail-service.xml
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/application-context.xml,
/WEB-INF/spring-database.xml,
/WEB-INF/spring-security.xml,
/WEB-INF/spring-web-config.xml,
/WEB-INF/mail-service.xml
</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
spring-web-config.xml:
<context:component-scan base-package="com.github.fedorchuck.morshinska" />
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/" />
<property name="suffix" value=".jsp" />
</bean>
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver"/>
<mvc:resources mapping="/resources/**" location="/resources/" cache-period="31556926"/>
<mvc:annotation-driven />
application-context.xml:
<bean id="springSecurityFilterChain" class="org.springframework.web.filter.DelegatingFilterProxy"/>
<bean id="userDetailsService" class="com.github.fedorchuck.morshinska.service.account.AccountDetailsServiceImpl"/>
<bean class="org.springframework.security.authentication.encoding.Md5PasswordEncoder" id="passwordEncoder"/>
<bean id="emailJDBCTemplate" class="com.github.fedorchuck.morshinska.dao.impl.EmailJDBCTemplate">
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="usersJDBCTemplate" class="com.github.fedorchuck.morshinska.dao.impl.UsersJDBCTemplate">
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="attachmentsToSendJDBCTemplate" class="com.github.fedorchuck.morshinska.dao.impl.AttachmentsToSendJDBCTemplate">
<property name="dataSource" ref="dataSource" />
</bean>
and log:
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping register
INFO: Mapped "{[/upload],methods=[GET]}" onto public java.lang.String com.github.fedorchuck.morshinska.web.controller.FileUploadController.showUploadForm(javax.servlet.http.HttpServletRequest)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping register
INFO: Mapped "{[/doUpload],methods=[POST]}" onto public java.lang.String com.github.fedorchuck.morshinska.web.controller.FileUploadController.handleFileUpload(javax.servlet.http.HttpServletRequest,org.springframework.web.multipart.commons.CommonsMultipartFile[]) throws java.lang.Exception
...
org.springframework.web.servlet.PageNotFound handleHttpRequestMethodNotSupported
WARNING: Request method 'POST' not supported
org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver logException
WARNING: Handler execution resulted in exception: Request method 'POST' not supported
Method GET working correct in contradistinction to POST (HTTP Status 405 - Request method 'POST' not supported). I'm sure - something wrong with configuration. So, where is my mistake. Please help.
The question is solved. It was CSRF token. More precisely:
<form method="POST"
enctype="multipart/form-data"
action="doUpload?${_csrf.parameterName}=${_csrf.token}">
...
<input type="hidden"
name="${_csrf.parameterName}"
value="${_csrf.token}" />
</form>
For more information chapter in docs.spring.io