Announcement Announcement Module
Collapse
No announcement yet.
Spring 3.0 annotated formatting: style/pattern not applied Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • #16
    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.

    Comment


    • #17
      Hi constv

      Thanks I shall continue to use the fmt tag.

      Comment


      • #18
        @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, 03:06 AM.

        Comment


        • #19
          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!

          Comment


          • #20
            Look at Greenhouse.

            Comment


            • #21
              Maybe everybody knew it already, so this message is for myself only: Joda time is needed if @DateTimeFormat is used or we'll see sth like:
              Code:
              20:15:10 DEBUG AbstractHandlerExceptionResolver:132 - Resolving exception from handler [home.OneController@1f5cbd6]: org.springframework.beans.TypeMismatchException: Failed to convert value of type 'java.lang.String' to required type 'java.util.Date'; nested exception is org.springframework.core.convert.ConversionFailedException: Unable to convert value "30-041982" from type 'java.lang.String' to type 'java.util.Date'; nested exception is java.lang.IllegalStateException: JodaTime library not available - @DateTimeFormat not supported
              20:15:10 DEBUG AbstractHandlerExceptionResolver:132 - Resolving exception from handler [home.OneController@1f5cbd6]: org.springframework.beans.TypeMismatchException: Failed to convert value of type 'java.lang.String' to required type 'java.util.Date'; nested exception is org.springframework.core.convert.ConversionFailedException: Unable to convert value "30-041982" from type 'java.lang.String' to type 'java.util.Date'; nested exception is java.lang.IllegalStateException: JodaTime library not available - @DateTimeFormat not supported
              20:15:10 DEBUG AbstractHandlerExceptionResolver:132 - Resolving exception from handler [home.OneController@1f5cbd6]: org.springframework.beans.TypeMismatchException: Failed to convert value of type 'java.lang.String' to required type 'java.util.Date'; nested exception is org.springframework.core.convert.ConversionFailedException: Unable to convert value "30-041982" from type 'java.lang.String' to type 'java.util.Date'; nested exception is java.lang.IllegalStateException: JodaTime library not available - @DateTimeFormat not supported
              20:15:10 DEBUG DispatcherServlet:824 - Null ModelAndView returned to DispatcherServlet with name 'test': assuming HandlerAdapter completed request handling
              20:15:10 DEBUG FrameworkServlet:699 - Successfully completed request
              The spring-mvc-showcase on github also says that.

              Comment


              • #22
                I've read that @DateTimeFormat is avaiable in Spring MVC, but I'm working with Spring Portlets. Is it avaiable for this case too? How I have to proceed? must I include <mvc:annotation-driven/> in my applicattion-context.xml file too?

                Now I'm printting my @DateTimeFormat date in a JSP without obtaining the expected result:


                @DateTimeFormat(pattern="dd-MM-yyyy")
                private java.util.Date myDate;

                and that's what I have printed: 2011-12-30 10:06:18.0 as result of <c:out value="${myForm.myDate}"></c:out>

                I've tried also with @DateTimeFormat(style="S-"), including and excluding the <mvc:annotation-driven/> and obtaining the same result, some help?

                Comment


                • #23
                  In Spring 3.1 @NumberFormatter does not allow pattern to be used for Currency or Percent types so the fractionDigits is a needed and missing annotation field. Furthermore rounding is always ROUND_HALF_EVEN (the default for BigDecimal) meaning rounding is a needed and missing annotation field. Another useful missing property is multiplier. That is the reason I had to come up with a CustomNumberFormatter and bunch of classes that I have documented at http://thinkinginsoftware.blogspot.c...on-driven.html, hopefully it will be useful as a tutorial to get custom Converters working in Spring.
                  Best,
                  -Nestor

                  Comment

                  Working...
                  X