Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make formatter value configurable through Spring EL/Property PlaceHolder [SPR-7087] #11747

Closed
spring-projects-issues opened this issue Apr 10, 2010 · 6 comments
Assignees
Labels
in: core Issues in core modules (aop, beans, core, context, expression) type: enhancement A general enhancement
Milestone

Comments

@spring-projects-issues
Copy link
Collaborator

spring-projects-issues commented Apr 10, 2010

Donny A. Wijaya opened SPR-7087 and commented

It would be great if we are allowed to configure the formatter (and validator) value through Spring EL or Property PlaceHolder
For example:

@DateTimeFormat(pattern="#{dateTime.pattern}")
private Date registrationDate;

and in application.properties loaded by Property PlaceHolder

dateTime.pattern=dd/mm/yyyy

Affects: 3.0.2

Attachments:

Issue Links:

Referenced from: commits 2a140ad

@spring-projects-issues
Copy link
Collaborator Author

Juergen Hoeller commented

Finally committed to trunk - will be available in tonight's 3.0.3 snapshot.

We simply support standard ${...} placeholders in formatting annotations now, like we did for @Scheduled already.

Juergen

@spring-projects-issues
Copy link
Collaborator Author

Donny A. Wijaya commented

Sample project to simulate the error

@spring-projects-issues
Copy link
Collaborator Author

Donny A. Wijaya commented

Hi Juergen,

I tried the new feature with the nightly 3.0.3 jar from maven repo. Unfortunately for me, I experienced this exception:

java.lang.IllegalArgumentException: Illegal pattern component: t
	org.joda.time.format.DateTimeFormat.parsePatternTo(DateTimeFormat.java:559)
	org.joda.time.format.DateTimeFormat.createFormatterForPattern(DateTimeFormat.java:682)
	org.joda.time.format.DateTimeFormat.forPattern(DateTimeFormat.java:170)
	org.springframework.format.datetime.joda.JodaDateTimeFormatAnnotationFormatterFactory.forPattern(JodaDateTimeFormatAnnotationFormatterFactory.java:122)
	org.springframework.format.datetime.joda.JodaDateTimeFormatAnnotationFormatterFactory.configureDateTimeFormatterFrom(JodaDateTimeFormatAnnotationFormatterFactory.java:111)
	org.springframework.format.datetime.joda.JodaDateTimeFormatAnnotationFormatterFactory.getPrinter(JodaDateTimeFormatAnnotationFormatterFactory.java:87)
	org.springframework.format.datetime.joda.JodaDateTimeFormatAnnotationFormatterFactory.getPrinter(JodaDateTimeFormatAnnotationFormatterFactory.java:1)
	org.springframework.format.support.FormattingConversionService$1.convert(FormattingConversionService.java:101)
	org.springframework.core.convert.support.ConversionUtils.invokeConverter(ConversionUtils.java:37)
	org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:187)
	org.springframework.validation.AbstractPropertyBindingResult.formatFieldValue(AbstractPropertyBindingResult.java:122)
	org.springframework.validation.AbstractBindingResult.getFieldValue(AbstractBindingResult.java:227)
	org.springframework.web.servlet.support.BindStatus.<init>(BindStatus.java:120)
	org.springframework.web.servlet.support.RequestContext.getBindStatus(RequestContext.java:726)
	sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
	sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
	java.lang.reflect.Method.invoke(Method.java:597)
	freemarker.ext.beans.BeansWrapper.invokeMethod(BeansWrapper.java:840)
	freemarker.ext.beans.OverloadedMethodModel.exec(OverloadedMethodModel.java:104)
	freemarker.core.MethodCall._getAsTemplateModel(MethodCall.java:93)
	freemarker.core.Expression.getAsTemplateModel(Expression.java:89)
	freemarker.core.Assignment.accept(Assignment.java:90)
	freemarker.core.Environment.visit(Environment.java:210)
	freemarker.core.IfBlock.accept(IfBlock.java:82)
	freemarker.core.Environment.visit(Environment.java:210)
	freemarker.core.MixedContent.accept(MixedContent.java:92)
	freemarker.core.Environment.visit(Environment.java:210)
	freemarker.core.Macro$Context.runMacro(Macro.java:172)
	freemarker.core.Environment.visit(Environment.java:603)
	freemarker.core.UnifiedCall.accept(UnifiedCall.java:106)
	freemarker.core.Environment.visit(Environment.java:210)
	freemarker.core.MixedContent.accept(MixedContent.java:92)
	freemarker.core.Environment.visit(Environment.java:210)
	freemarker.core.Macro$Context.runMacro(Macro.java:172)
	freemarker.core.Environment.visit(Environment.java:603)
	freemarker.core.UnifiedCall.accept(UnifiedCall.java:106)
	freemarker.core.Environment.visit(Environment.java:210)
	freemarker.core.MixedContent.accept(MixedContent.java:92)
	freemarker.core.Environment.visit(Environment.java:210)
	freemarker.core.Environment.process(Environment.java:190)
	freemarker.template.Template.process(Template.java:237)
	org.springframework.web.servlet.view.freemarker.FreeMarkerView.processTemplate(FreeMarkerView.java:366)
	org.springframework.web.servlet.view.freemarker.FreeMarkerView.doRender(FreeMarkerView.java:283)
	org.springframework.web.servlet.view.freemarker.FreeMarkerView.renderMergedTemplateModel(FreeMarkerView.java:233)
	org.springframework.web.servlet.view.AbstractTemplateView.renderMergedOutputModel(AbstractTemplateView.java:167)
	org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:250)
	org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1060)
	org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:798)
	org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:716)
	org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)
	org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:549)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:690)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
	com.opensymphony.sitemesh.webapp.SiteMeshFilter.obtainContent(SiteMeshFilter.java:129)
	com.opensymphony.sitemesh.webapp.SiteMeshFilter.doFilter(SiteMeshFilter.java:77)
	org.tuckey.web.filters.urlrewrite.NormalRewrittenUrl.doRewrite(NormalRewrittenUrl.java:195)
	org.tuckey.web.filters.urlrewrite.RuleChain.handleRewrite(RuleChain.java:159)
	org.tuckey.web.filters.urlrewrite.RuleChain.doRules(RuleChain.java:141)
	org.tuckey.web.filters.urlrewrite.UrlRewriter.processRequest(UrlRewriter.java:90)
	org.tuckey.web.filters.urlrewrite.UrlRewriteFilter.doFilter(UrlRewriteFilter.java:417)
	org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
	org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)

I've attached the zip to simulate this error in previous post. In User.java, I used ${dateTime.pattern} for DateTimeFormat's pattern. In the application.properties, I have dateTime.pattern property with value MM/dd/yyyy. The error happened when I opened the User Add form at: http://localhost:8080/basic/admin/user/add

@spring-projects-issues
Copy link
Collaborator Author

Juergen Hoeller commented

Good catch - things only worked with a plain FormattingConversionService; we were missing FormattingConversionServiceFactoryBean (as used by the mvc namespace). Fixed for tonight's 3.0.3 snapshot...

Juergen

@spring-projects-issues
Copy link
Collaborator Author

Donny A. Wijaya commented

Hi Juergen,

It works!! I tested on both field and method level. Thanks a ton for this nice feature :)

Donny

@spring-projects-issues
Copy link
Collaborator Author

adrian commented

Hi, from what I've tested, this issue resolves values only from placeholders.

Could it be enhanced to resolve from SpEl :

  • more powerfull.
  • would enable internationalization for french locale ("S-" style shows only 2 digits for year which is a no-go for us).

For more information, see http://forum.springsource.org/showthread.php?113518-DateTimeFormat-and-pattern-i18n.

WDYT ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: core Issues in core modules (aop, beans, core, context, expression) type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

2 participants