O instrutor usou a classe Calendar para armazenar a data de lançamento. Como essa biblioteca é antiga, tentei usar LocalDate e não deu certo seguindo os mesmos passos da aula.
A solução foi a seguinte:Criar dois Converter, de String para LocalDate e vice-versa.
Minha implementação ficou assim:
package br.com.casadocodigo.loja.conf;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import org.springframework.core.convert.converter.Converter;
public class LocalDateToString implements Converter<LocalDate, String> {
@Override
public String convert(LocalDate source) {
return source.format(DateTimeFormatter.ofPattern("dd/MM/yyyy"));
}
}
package br.com.casadocodigo.loja.conf;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import org.springframework.core.convert.converter.Converter;
public class StringToLocalDate implements Converter<String, LocalDate> {
@Override
public LocalDate convert(String from) {
try {
LocalDate date = LocalDate.parse(from, DateTimeFormatter.ofPattern("dd/MM/yyyy"));
return date;
} catch (DateTimeParseException ex) {
return null;
}
}
}
package br.com.casadocodigo.loja.conf;
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
import org.springframework.format.FormatterRegistry;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import br.com.casadocodigo.loja.controllers.HomeController;
/**
*
* @author roinujnosde
*/
@EnableWebMvc
@ComponentScan(basePackageClasses = {HomeController.class})
public class AppWebConfiguration implements WebMvcConfigurer {
@Override
public void addFormatters(FormatterRegistry registry) {
registry.addConverter(new StringToLocalDate());
registry.addConverter(new LocalDateToString());
}
@Bean
public MessageSource messageSource() {
ReloadableResourceBundleMessageSource source = new ReloadableResourceBundleMessageSource();
source.setDefaultEncoding("UTF-8");
source.setBasename("/WEB-INF/messages");
source.setCacheSeconds(1);
return source;
}
//
// @Bean
// public FormattingConversionService mvcConversionService() {
// FormattingConversionService conversionService = new FormattingConversionService();
// DateFormatterRegistrar registrar = new DateFormatterRegistrar();
// registrar.setFormatter(new DateFormatter("dd/MM/yyyy"));
// registrar.registerFormatters(conversionService);
//
// return conversionService;
// }
//
@Bean
public InternalResourceViewResolver internalResourceViewResolver() {
InternalResourceViewResolver resolver =
new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/views/");
resolver.setSuffix(".jsp");
return resolver;
}
}