Boa tarde, estou com 2 problemas:
1- Fiz a implementação de configuração do MultipartFile
Minha classe de configuração do Sevlet:
package br.com.gerenciador.config;
import javax.servlet.MultipartConfigElement;
import javax.servlet.ServletRegistration.Dynamic;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
public class AppConfig extends AbstractAnnotationConfigDispatcherServletInitializer{
@Override
protected Class<?>[] getRootConfigClasses() {
return null;
}
@Override
protected Class<?>[] getServletConfigClasses() {
return new Class<?>[]{AppConfigMVC.class, JPAConfig.class};
}
@Override
protected String[] getServletMappings() {
return new String[]{"/"};
}
@Override
protected void customizeRegistration(Dynamic registration) {
registration.setMultipartConfig(new MultipartConfigElement(""));
}
}
Minha classe de configuração dos Beans:
package br.com.gerenciador.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.format.datetime.DateFormatter;
import org.springframework.format.datetime.DateFormatterRegistrar;
import org.springframework.format.support.DefaultFormattingConversionService;
import org.springframework.format.support.FormattingConversionService;
import org.springframework.web.multipart.MultipartResolver;
import org.springframework.web.multipart.support.StandardServletMultipartResolver;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import com.gerenciador.dao.DAONewsImpl;
import br.com.gerenciador.controllers.HomeController;
import br.com.gerenciador.rn.RNAddNews;
import br.com.gerenciador.service.Facade;
@EnableWebMvc
@ComponentScan(basePackageClasses={
HomeController.class,
DAONewsImpl.class,
RNAddNews.class,
Facade.class
})
public class AppConfigMVC extends WebMvcConfigurerAdapter{
@Bean
public InternalResourceViewResolver viewResolver(){
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/views/");
resolver.setSuffix(".jsp");
return resolver;
}
@Bean
public FormattingConversionService serviceConversionDate() {
/**
* Responsável pelo serviço de conversão
*/
DefaultFormattingConversionService conversionService = new DefaultFormattingConversionService();
/**
* Registra o formato da data para conversão
*/
DateFormatterRegistrar registerFormat = new DateFormatterRegistrar();
registerFormat.setFormatter(new DateFormatter("dd/MM/yyyy"));
registerFormat.registerFormatters(conversionService);
return conversionService;
}
@Bean
public MultipartResolver multipartResolver(){
return new StandardServletMultipartResolver();
}
@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
}
Até ai quando tento usar o MultpartFile na minha classe de controle para imprimir o nome do meu arquivo ele está ainda continua dando
NullPointExeption
SEVERE: Servlet.service() for servlet [dispatcher] in context with path [/gerenciador] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause
java.lang.NullPointerException
at br.com.gerenciador.controllers.NewsController.gravar(NewsController.java:25)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:220)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:134)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:116)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:963)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:897)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:872)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:292)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:212)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:94)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:504)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:620)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:509)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1104)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:684)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1524)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1480)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Unknown Source)
Daí eu pensei pq não tinha colocado oenctype="multipart/form-data"
na minha JSP, porém quando coloco, ele da erro 400
HTTP Status 400 -
type Status report
message
description The request sent by the client was syntactically incorrect.
Apache Tomcat/8.0.41
MInha JSP
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="UTF-8"%>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
<%@taglib uri="http://www.springframework.org/tags" prefix="s" %>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<!--Import Google Icon Font-->
<link href="http://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
<!--Import materialize.css-->
<link type="text/css" rel="stylesheet" href="../resources/css/materialize.min.css" media="screen,projection"/>
<!--Let browser know website is optimized for mobile-->
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<meta http-equiv="Content-Type" content="text/html; UTF-8">
<title>Gerenciador</title>
</head>
<body>
<h1 class="center">News Add</h1>
<div class="container">
<form:form action="${s:mvcUrl('NC#gravar').build()}" enctype="multipart/form-data" method="POST">
<div class="row">
<div class="input-field col s12 m12 l12">
<input id="title" type="text" name="title">
<label for="title">Title</label>
</div>
<div class="input-field col s12 m12 l12">
<textarea id="textnews" class="materialize-textarea" name="text" rows="50" cols="100"></textarea>
<label for="textnews">Content</label>
</div>
<div class="input-field col s12 m12 l12">
<input id="video" type="text" name="linkVideo">
<label for="video">Video(YouTube)</label>
</div>
<div class="col s12 m12 l12" style="padding-bottom: 40px; padding-top: 20px;">
<input id="file" type="file" name="pathImage">
</div>
<button type="submit" class="waves-effect waves-light btn">Add</button>
</div>
</form:form>
</div>
<!--Import jQuery before materialize.js-->
<script type="text/javascript" src="../resources/js/jquery-3.1.1.js"></script>
<script type="text/javascript" src="../resources/js/materialize.min.js"></script>
</body>
</html>
Meu Controller
package br.com.gerenciador.controllers;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.multipart.MultipartFile;
import com.gerenciador.models.News;
@Controller
@RequestMapping("/news")
public class NewsController {
/*@Autowired
private Facade facade;*/
@RequestMapping(value = "/add", method=RequestMethod.GET)
public String formCadastrar(){
return "news/add";
}
@RequestMapping(value = "/gravar", method=RequestMethod.POST)
public String gravar(MultipartFile file, News pNews){
System.out.println("File: "+file.getOriginalFilename());
System.out.println(pNews);
//facade.addNews(pNews);
return "news/adicionado";
}
}