Page 1 of 2 12 LastLast
Results 1 to 10 of 15

Thread: Formatting for dates/numbers

  1. #1
    Join Date
    Nov 2010
    Posts
    24

    Default Formatting for dates/numbers

    Hi

    I'm moving my project to Spring 3, and trying to use its resources extensively. Now I'm trying to set up Date and BigDecimal formatters. Using 2.5, I created an initBinder and registered a custom editor on every controller, and used <fmt> on every field on JSP. I really dislike that solution, is too much intrusive, IMHO.

    I'm trying to keep it simple and flexible, but so far no good. I wish to use annotations configurations (like @DateTimeFormat on Date fields, setting properties as needed - usually "S-") to keep it flexible, but avoid repetition (repeating the format everywhere). Using this annotation, my command field is being incorrectly parsed and incorrectly displayed back on the interface. Guess this should be because I need to set a locale, so I tried setting a FixedLocaleResolver like this:

    Code:
    	<bean id="localeResolver" class="org.springframework.web.servlet.i18n.FixedLocaleResolver">
    		<property name="defaultLocale" value="xx_YY"/>
    	</bean>
    It didn't worked (need to inject it somewhere? I couldn't find it). Guess I could use a pattern for the annotation, but I prefer something simpler and more flexible if someday my app becomes multilanguage. What am I missing to make the annotation work as expected?

    Also, I noticed that using style="S-" is formatting the year as 2 digits, and I want 4 digits for all my dates system-wide. If in my locale the year is also 2 digit, can I tweak it to 4 digit somehow in a simple way?
    Similarly, when dealing with numbers (BigDecimal), I'll want to parse and print numbers without grouping separator on input fields, despite the locale format probably including the separator, as I believe they may lead to confusion (some folks using grouping separator as digit separator).

    So, can I do these tasks with standard Spring annotation, or maybe I'll need to create my own formatter annotations?

    Thanks!

  2. #2
    Join Date
    Nov 2010
    Posts
    24

    Default

    I did some further testing, and the FixedLocaleResolver is working nicely with the code above, but still I haven't got the result I want.

    In my locale, if I use style "S-" for dates, I get two letter year, instead of four letter. And for BigDecimal, it uses grouping separators, and I'll need them out.

    So, except for defining a pattern for every @DateTimeFormat/@NumberFormat, what is the most flexible solution? Is there some XML config that I can add or I'll a custom annotation?

    Thanks!

  3. #3

    Default

    if you only want to display date, you can use the tag <fmt:formatDate type="date" value="${command.property}"/>

    but I don't know how to display a date in an input field correctly. In my case, all date displayed in form are unformatted.
    I tried spring:bind but it doesn't work out.

    I am trying spring:eval.... Hope I can get some good news..

  4. #4
    Join Date
    Nov 2010
    Posts
    24

    Default

    My old code used <fmt:formatDate>, but it's cumbersome, plus annotation String annotation formatters do the job two-way (parse and format), so I'd like to just tweak it a little to fit my needs.
    Anyone?

    About your issue, if you want to keep using <fmt> tags, you can apply them to input fields as well. If your problem is that you can't insert the <fmt:formatDate> tag inside the <form:input>, save the formatted result to a variable with the 'var' attribute, then just use it for the input tag value with ${variableNameHere}.
    That's how I did for my old code... it was a crappy code, so it may be a bad way to do it.

    But I suggest you to use formatter annotations on the fields.

  5. #5

    Default

    A good reference for DateTimeFormat:
    http://forum.springsource.org/showthread.php?t=83220

  6. #6

    Default

    Thanks. I will try that.
    I am using Spring Webflow 2.2.1.In the reference, they claim that SWF "uses the type conversion and formatting system introduced in Spring 3 for nearly all type conversion needs." But I just cannot get the datetimeformat annotation work.

    Quote Originally Posted by mdrg View Post
    My old code used <fmt:formatDate>, but it's cumbersome, plus annotation String annotation formatters do the job two-way (parse and format), so I'd like to just tweak it a little to fit my needs.
    Anyone?

    About your issue, if you want to keep using <fmt> tags, you can apply them to input fields as well. If your problem is that you can't insert the <fmt:formatDate> tag inside the <form:input>, save the formatted result to a variable with the 'var' attribute, then just use it for the input tag value with ${variableNameHere}.
    That's how I did for my old code... it was a crappy code, so it may be a bad way to do it.

    But I suggest you to use formatter annotations on the fields.

  7. #7
    Join Date
    Nov 2010
    Posts
    24

    Default

    Thanks for the link, good reading.

    But I don't use Webflow, and my formatters are working, except that I need a bit more customization on them.

    Still waiting for some clarifying help!
    Thanks!

  8. #8

    Default

    Have you tried pattern or ISO date formats?
    @DateTimeFormat(pattern="yyyy-MM-dd")
    or
    @DateTimeFormat(iso=ISO.DATE)

    Quote Originally Posted by mdrg View Post
    Hi

    I'm moving my project to Spring 3, and trying to use its resources extensively. Now I'm trying to set up Date and BigDecimal formatters. Using 2.5, I created an initBinder and registered a custom editor on every controller, and used <fmt> on every field on JSP. I really dislike that solution, is too much intrusive, IMHO.

    I'm trying to keep it simple and flexible, but so far no good. I wish to use annotations configurations (like @DateTimeFormat on Date fields, setting properties as needed - usually "S-") to keep it flexible, but avoid repetition (repeating the format everywhere). Using this annotation, my command field is being incorrectly parsed and incorrectly displayed back on the interface. Guess this should be because I need to set a locale, so I tried setting a FixedLocaleResolver like this:

    Code:
    	<bean id="localeResolver" class="org.springframework.web.servlet.i18n.FixedLocaleResolver">
    		<property name="defaultLocale" value="xx_YY"/>
    	</bean>
    It didn't worked (need to inject it somewhere? I couldn't find it). Guess I could use a pattern for the annotation, but I prefer something simpler and more flexible if someday my app becomes multilanguage. What am I missing to make the annotation work as expected?

    Also, I noticed that using style="S-" is formatting the year as 2 digits, and I want 4 digits for all my dates system-wide. If in my locale the year is also 2 digit, can I tweak it to 4 digit somehow in a simple way?
    Similarly, when dealing with numbers (BigDecimal), I'll want to parse and print numbers without grouping separator on input fields, despite the locale format probably including the separator, as I believe they may lead to confusion (some folks using grouping separator as digit separator).

    So, can I do these tasks with standard Spring annotation, or maybe I'll need to create my own formatter annotations?

    Thanks!

  9. #9
    Join Date
    Nov 2010
    Posts
    24

    Default

    ISO is not suitable, it is intended for standard visual representation, and as said in the topic, I specifically do not want to use a pattern, because I'll have to repeat it everywhere, and because it is not multi-locale-friendly (every locale would need a different pattern).

  10. #10
    Join Date
    Aug 2004
    Location
    Melbourne, FL
    Posts
    2,794

    Default

    What are your requirements exactly?

    Are you saying you'd like the default String<->Date formatting rule to be yyyy-mm-dd (adjusting the pattern for the current locale)? This is definitely possible. Would you expect this rule to be enforced only when using @DateTimeFormat, or would you view the annotation as entirely optional? You have the ability for conversion to be driven by annotation or by type only (e.g. String to Date, String to BigDecimal).

    If you can be very specific about what you're trying to achieve, I can show you how to do it with the current system. It's a pretty flexible system.

    Keith
    Last edited by Keith Donald; Dec 8th, 2010 at 08:26 PM.
    Keith Donald
    Core Spring Development Team

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •