Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in / Register
Toggle navigation
S
spring-boot
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
DEMO
spring-boot
Commits
0f738be1
Commit
0f738be1
authored
May 04, 2014
by
Marcel Overdijk
Committed by
Dave Syer
May 04, 2014
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add specifying (fixed) date format via application properties
Fixes gh-778, Fixes gh-755
parent
94987195
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
45 additions
and
0 deletions
+45
-0
WebMvcAutoConfiguration.java
...ework/boot/autoconfigure/web/WebMvcAutoConfiguration.java
+11
-0
WebMvcAutoConfigurationTests.java
.../boot/autoconfigure/web/WebMvcAutoConfigurationTests.java
+33
-0
appendix-application-properties.adoc
...cs/src/main/asciidoc/appendix-application-properties.adoc
+1
-0
No files found.
spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/WebMvcAutoConfiguration.java
View file @
0f738be1
...
...
@@ -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
)
{
...
...
spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/WebMvcAutoConfigurationTests.java
View file @
0f738be1
...
...
@@ -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
();
...
...
spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc
View file @
0f738be1
...
...
@@ -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
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment