Page 2 of 3 FirstFirst 123 LastLast
Results 11 to 20 of 23

Thread: Spring 3.0 annotated formatting: style/pattern not applied

  1. #11
    Join Date
    Nov 2006
    Location
    Boston, MA
    Posts
    303

    Default

    Are you just placing the list directly into the request? That won't work the way you are doing it because Spring is looking for a method "get${yourList}()" on your form-backing bean, and not finding it. In the example I gave you above, it is assumed that your model attribute's name is "yourModel", i.e. a form-backing bean with such name must be present in the request scope, so your JSP code can also access it directly. That form bean must contain a collection property, e.g. "yourList", and the getter method for it. If you do it the way I showed you, it should absolutely work.

    The JSTL's <fmt:formatDate> tag supports locales. You can set the locale explicitly:
    Code:
    <fmt:setLocale value="de" />
    and that will always override the client's browser's settings. If the locale is not explicitly set, the date should be formatted according to the client's browser's settings, I believe. So, if you do something like this:

    Code:
      <fmt:formatDate dateStyle="SHORT" value="${item.yourDate}" />
    it should display it according to the user's locale settings.

    HTH,
    Constantine

  2. #12

    Default Thanks

    Hi constv,

    Many thanks for your persistence with this.

    My locale can be changed dynamically so using the fmt tag in the manner you described should be sufficient!

    Thank you for the help.

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

    Default

    Hi guys,

    I was just pointed to this thread, sorry for not noticing it earlier.

    Constantine, you are correct about Web Flow 2.0.x not knowing anything about the new Spring Framework 3 type conversion system. The Spring 3 system was heavily inspired by Web Flow, but is not the same. Web Flow 3 M1 coming out next month will provide a version of Web Flow designed to work with the new system... we're working on that now (appreciate your patience while that project catches up with the Spring 3 foundation).

    As far as working with plain MVC @Controllers, that should work fine, as long as you're accessing such annotated properties via the Spring binding system e.g. via a form:input tag. Plain JSTL ${expressions} just use the default EL and are not aware of our conversion system (In 3.1 we expect to enhance SpEL to work in this environment then this will work). For a sample showing this, see my blog here http://blog.springsource.com/2009/12...in-spring-3-0/.

    Now, I do need to correct some misinformation in this thread. @DateTimeFormat DOES work with plain java.util.Date, Calendar, and Long types, as described in the reference documentation. The default conversion message is not pretty but you can customize it through use of a MessageSource. The sample referenced in the blog shows this being done with a general 'typeMismatch' error code, but you could also use a property specific or type-specific code (e.g. typeMismatch.account.renewalDate or typeMismatch.java.util.Date).

    I hope this clarifies things a little,

    Keith
    Keith Donald
    Core Spring Development Team

  4. #14
    Join Date
    Nov 2006
    Location
    Boston, MA
    Posts
    303

    Default

    Keith, thanks for the clarifications, and thanks for the much anticipated update on the release of SWF 3M1. That's great news!

  5. #15

    Default

    Hi Keith and constv

    Thank you very much for this information.

    I guess the main flaw in my code was the fact I was using JSTL expressions and thus would never invoke any annotation rendering I had specified.

    I will look at using the spring form tag library to try and reference to object attributes that way.

    Currently the items I have displaying are just listed in a table and are not editable so to speak, to implement this would I still need to use the Spring form tag library such as the form:label field or can I use some other spring library such as spring:bind?

    Again thanks for all the information

  6. #16
    Join Date
    Nov 2006
    Location
    Boston, MA
    Posts
    303

    Default

    eggsy,
    as I have mentioned in a previous reply, if you are only displaying read-only values on a page, there is no need at all for you to do Spring bindings. What for? Only to make sure that the supplementary formatting is engaged? You are not reading the values back, so you don't need to bind anything. Such page should use simple JSP EL and, if necessary, JSTL. That's it. Strings that do not need any formatting will be rendered using the EL:
    Code:
    ${requestScope.modelName.propName}
    Use JSTL's formatting for your dates, etc. It's as simple as that. Based on what Keith is saying, SpEL in Spring 3.1 should be able to support formatting/conversion in ${} expressions, so things would become even simpler.

  7. #17

    Default

    Hi constv

    Thanks I shall continue to use the fmt tag.

  8. #18
    Join Date
    Dec 2008
    Location
    Ulaanbaatar, Mongolia
    Posts
    123

    Default

    @DateTimeFormat in form model still not working for me.
    Am I doing something wrong?

    Following working for me, But it's not that effective because I want to use dates in "yyyy-MM-dd HH:mm" and "yyyy-MM-dd" both. Now all my date inputs require "yyyy-MM-dd HH:mm".
    Code:
    package com.mobinet.wos.util.spring;
    
    import java.util.Date;
    
    import org.joda.time.DateTime;
    import org.springframework.format.FormatterRegistry;
    import org.springframework.format.datetime.DateFormatter;
    import org.springframework.format.support.FormattingConversionServiceFactoryBean;
    import org.springframework.stereotype.Component;
    
    @Component("applicationConversionService")
    public class ApplicationConversionServiceFactoryBean extends FormattingConversionServiceFactoryBean {
    
    	protected void installFormatters(FormatterRegistry registry) {
    		super.installFormatters(registry);
    		registry.addFormatterForFieldType(Date.class, new DateFormatter("yyyy-MM-dd HH:mm"));
    	}
    }
    BTW I'm using spring web flow 2.2.1, spring 3.0.5 and joda time 1.6.

    Edit: I finally got it working @DateTimeFormat. Install JodaDateTimeFormatAnnotationFormatterFactory in conversion service.

    Code:
    package com.mobinet.wos.util.spring;
    
    import org.springframework.format.FormatterRegistry;
    import org.springframework.format.datetime.joda.JodaDateTimeFormatAnnotationFormatterFactory;
    import org.springframework.format.support.FormattingConversionServiceFactoryBean;
    import org.springframework.stereotype.Component;
    
    @Component("applicationConversionService")
    public class ApplicationConversionServiceFactoryBean extends FormattingConversionServiceFactoryBean {
    
    	protected void installFormatters(FormatterRegistry registry) {
    		super.installFormatters(registry);
    		registry.addFormatterForFieldAnnotation(new JodaDateTimeFormatAnnotationFormatterFactory());
    	}
    }
    Last edited by digz6666; Jan 14th, 2011 at 03:06 AM.
    Let's care our nature!

  9. #19
    Join Date
    Apr 2010
    Posts
    9

    Default

    hello Keith,

    I was searching for similar problem, and found this page. Now that Spring 3.1 is in M1, can you point to an example on how we can do this in 3.1?

    I tried reading 3.1M1 documentation but could not find an example.

    Thanks!

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

    Default

    Look at Greenhouse.
    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
  •