Adicionei o MultipartResolver na classe AppWebConfiguration, mas continua mostrando o erro de request.
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.IllegalArgumentException: Expected MultipartHttpServletRequest: is a MultipartResolver configured? org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:894) org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:789) javax.servlet.http.HttpServlet.service(HttpServlet.java:660) javax.servlet.http.HttpServlet.service(HttpServlet.java:741) org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
Class AppWebConfiguration
@EnableWebMvc
@ComponentScan(basePackageClasses = {HomeController.class,ProdutoDAO.class})
public class AppWebConfiguration {
@Bean
public InternalResourceViewResolver internalResourceViewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/views/");//setPrefix define o caminho onde estão nossas views
resolver.setSuffix(".jsp"); //setSuffix adiciona a extensão dos arquivos views.
return resolver;
}
@Bean
public MessageSource messageSource() {
ReloadableResourceBundleMessageSource messageSource =
new ReloadableResourceBundleMessageSource();
messageSource.setBasename("/WEB-INF/messages");
messageSource.setDefaultEncoding("UTF-8");
messageSource.setCacheSeconds(1);
return messageSource;
}
@Bean
public FormattingConversionService mvcConversionService(){
DefaultFormattingConversionService conversionService = new DefaultFormattingConversionService();
DateFormatterRegistrar formatterRegistrar = new DateFormatterRegistrar();
formatterRegistrar.setFormatter(new DateFormatter("dd/MM/yyyy"));
formatterRegistrar.registerFormatters(conversionService);
return conversionService;
}
@Bean
public MultipartResolver multipartResolver(){
return new StandardServletMultipartResolver();
}
}
Class ServletSpring MVC
public class ServletSpringMVC extends AbstractAnnotationConfigDispatcherServletInitializer{
@Override
protected Class<?>[] getRootConfigClasses() {
// TODO Auto-generated method stub
return null;
}
@Override
protected Class<?>[] getServletConfigClasses() {
// TODO Auto-generated method stub
return new Class[] {AppWebConfiguration.class, JPAConfiguration.class};
}
@Override
protected String[] getServletMappings() {
// TODO Auto-generated method stub
return new String[] {"/"};
}
@Override
protected Filter[] getServletFilters() {
CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter();
// characterEncodingFilter.setEncoding("UTF-8");
return new Filter[] {characterEncodingFilter};
}
@Override
protected void customizeRegistration(Dynamic registration) {
registration.setMultipartConfig(new MultipartConfigElement(""));
}
}
O método gravar da Class ProdutosController
@RequestMapping(method = RequestMethod.POST)
public ModelAndView gravar( MultipartFile sumario, @Valid Produto produto, Preco preco, BindingResult result,
RedirectAttributes redirectAttributes) throws SQLException{
System.out.println(sumario.getOriginalFilename());
if(result.hasErrors()) {
return form(produto);
}
System.out.println(produto);
produtoDao.gravar(produto,preco);
redirectAttributes.addFlashAttribute("message","Produto cadastrado com sucesso!");
return new ModelAndView("redirect:produtos");
}