Add specifying (fixed) date format via application properties

Fixes gh-778, Fixes gh-755
pull/776/merge
Marcel Overdijk 11 years ago committed by Dave Syer
parent 949871957b
commit 0f738be114

@ -20,6 +20,7 @@ import java.io.IOException;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import javax.servlet.Servlet;
@ -48,6 +49,7 @@ import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.format.Formatter;
import org.springframework.format.FormatterRegistry;
import org.springframework.format.datetime.DateFormatter;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.util.StringUtils;
import org.springframework.validation.DefaultMessageCodesResolver;
@ -143,6 +145,9 @@ public class WebMvcAutoConfiguration {
@Value("${spring.mvc.locale:}")
private String locale = "";
@Value("${spring.mvc.date-format:}")
private String dateFormat = "";
@Autowired
private ListableBeanFactory beanFactory;
@ -200,6 +205,12 @@ public class WebMvcAutoConfiguration {
return new FixedLocaleResolver(StringUtils.parseLocaleString(this.locale));
}
@Bean
@ConditionalOnExpression("'${spring.mvc.date-format:}' != ''")
public Formatter<Date> dateFormatter() {
return new DateFormatter(this.dateFormat);
}
@Override
public MessageCodesResolver getMessageCodesResolver() {
if (this.messageCodesResolverFormat != null) {

@ -17,6 +17,7 @@
package org.springframework.boot.autoconfigure.web;
import java.lang.reflect.Field;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
@ -25,6 +26,7 @@ import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.joda.time.DateTime;
import org.junit.After;
import org.junit.Rule;
import org.junit.Test;
@ -41,6 +43,7 @@ import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.format.support.FormattingConversionService;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.util.ReflectionUtils;
import org.springframework.util.StringUtils;
@ -152,6 +155,7 @@ public class WebMvcAutoConfigurationTests {
equalTo((Resource) new ClassPathResource("/foo/")));
}
@Test
public void noLocaleResolver() throws Exception {
this.context = new AnnotationConfigEmbeddedWebApplicationContext();
this.context.register(AllResources.class, Config.class,
@ -183,6 +187,35 @@ public class WebMvcAutoConfigurationTests {
assertThat(locale.toString(), equalTo("en_UK"));
}
@Test
public void noDateFormat() throws Exception {
this.context = new AnnotationConfigEmbeddedWebApplicationContext();
this.context.register(AllResources.class, Config.class,
WebMvcAutoConfiguration.class,
HttpMessageConvertersAutoConfiguration.class,
PropertyPlaceholderAutoConfiguration.class);
this.context.refresh();
FormattingConversionService cs = this.context.getBean(FormattingConversionService.class);
Date date = new DateTime(1988, 6, 25, 20, 30).toDate();
// formatting cs should use simple toString()
assertThat(cs.convert(date, String.class), equalTo(date.toString()));
}
@Test
public void overrideDateFormat() throws Exception {
this.context = new AnnotationConfigEmbeddedWebApplicationContext();
// set fixed date format
EnvironmentTestUtils.addEnvironment(this.context, "spring.mvc.date-format:dd*MM*yyyy");
this.context.register(AllResources.class, Config.class,
WebMvcAutoConfiguration.class,
HttpMessageConvertersAutoConfiguration.class,
PropertyPlaceholderAutoConfiguration.class);
this.context.refresh();
FormattingConversionService cs = this.context.getBean(FormattingConversionService.class);
Date date = new DateTime(1988, 6, 25, 20, 30).toDate();
assertThat(cs.convert(date, String.class), equalTo("25*06*1988"));
}
@Test
public void noMessageCodesResolver() throws Exception {
this.context = new AnnotationConfigEmbeddedWebApplicationContext();

@ -68,6 +68,7 @@ content into your application; rather pick only the properties that you need.
http.mappers.json-pretty-print=false # pretty print JSON
http.mappers.json-sort-keys=false # sort keys
spring.mvc.locale= # set fixed locale, e.g. en_UK
spring.mvc.date-format= # set fixed date format, e.g. dd/MM/yyyy
spring.mvc.message-codes-resolver.format= # PREFIX_ERROR_CODE / POSTFIX_ERROR_CODE
spring.view.prefix= # MVC view prefix
spring.view.suffix= # ... and suffix

Loading…
Cancel
Save