Page 1 of 3 123 LastLast
Results 1 to 10 of 23

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

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

    Default Spring 3.0 annotated formatting: style/pattern not applied

    I am trying to use the new 3.0 formatting annotations, such as @DateTimeFormat - exactly as demonstrated in Keith's blog and reference guide, which seems to be quite straightforward. I have carefully read the guidelines, and I think I have put in place all the necessary (basic) configuration (<mvc:annotation-driven/> etc. ) However, the formatting does not seem to be applied. For example, on the JSP the date is displayed in the "Mon Jan 1 YYYY ..." format (basic Date.toString() representation) inside a Spring <form:form> tag or via ${dateProperty} - regardless of the style or pattern used in the annotation for the property - on a non-flow page.

    Could this possibly have anything to do with the fact that the same application also uses Spring Webflow 2.0.8, and an instance of the old-style (webflow 2.x) conversion service is registered within the app context - not under the default "conversionService" name but explicitly under a different name - specifically for SWF to use? The latter is necessary since the currently available SWF release is not compatible with the new Spring 3.0 conversion service. The flow pages display the dates correctly because I specify the appropriate custom formatter within the "binding" attribute in the view state definitions. My understanding is that adding "<mvc:annotation-driven/>" to the MVC configuration will create an inner default 3.0 configuration service bean within the MVC namespace, and that conversion will be used for data binding - outside the flows, while the flows will use the SWF-specific conversion service that I have explicitly defined for the flow building services. Is this correct? No formatting is applied to my date values outside the flows. Why? The binding seems to take place since the data is displayed and read back. Can anyone provide any helpful thoughts on this?

    Thanks,
    Constantine
    Last edited by constv; Jan 15th, 2010 at 03:16 PM.

  2. #2

    Default Same problem

    Hi there,

    I have also experienced the same problem?

    Just to confirm I have the following important elements:

    mvc-config.xml

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:p="http://www.springframework.org/schema/p"
           xmlns:context="http://www.springframework.org/schema/context"
           xmlns:mvc="http://www.springframework.org/schema/mvc"
           xsi:schemaLocation="http://www.springframework.org/schema/beans 
                               http://www.springframework.org/schema/beans/spring-beans.xsd
                               http://www.springframework.org/schema/context 
                               http://www.springframework.org/schema/context/spring-context.xsd
                               http://www.springframework.org/schema/mvc 
                               http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
                               
    	<mvc:annotation-driven/>
    
    etc....
    Annotated field

    Code:
    	@DateTimeFormat(style="S-")
    	public Date getSoftDeletedDate() {
    		return softDeletedDate;
    	}
    JSP snippet:

    Code:
    <td>${myObject.softDeletedDate}</td>
    But this outputs the following:

    2010-01-21 00:00:00.0

    Am I missing anything ? Or do you have to render the object differently on the JSP page?
    Last edited by eggsy84; Jan 21st, 2010 at 11:06 AM. Reason: paragraph not required

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

    Default

    eggsy, you need to make sure that Spring bindings actually take place. When you are using plain JSP EL to output the value, e.g. ${propertyname}, it just renders the toString() representation of your date property. Try to display the value as an input field within a <form:form> to ensure the bindings, and see what happens.

    I have never been able to get the annotated formatting to work in my case, by the way, so I gave up on that, needed to move on. I reverted back to using the @InitBinder-annotated method in the controller that configures a CustomDateEditor for all dates. Or, you can configure that globally in the XML. (In my current case, only one controller needs that.) That worked fine for me, and I continue to use that. I would like to see some good/complete/working examples of using @DateTimeFormat annotations, etc. So far, there aren't any in the on-line Spring samples (at least, as of the last time I checked - a couple of days ago.)

  4. #4
    Join Date
    Aug 2004
    Location
    London, UK
    Posts
    36

    Default

    @DateTimeFormat only works with org.joda.time.DateTime types, from JodaTime framework, and not with java.util.Date. That seems to cause some other issues, like: http://forum.springsource.org/showpo...00&postcount=9
    Ale Sarco

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

    Default

    Quote Originally Posted by asarco View Post
    @DateTimeFormat only works with org.joda.time.DateTime types, from JodaTime framework, and not with java.util.Date. That seems to cause some other issues, like: http://forum.springsource.org/showpo...00&postcount=9
    Thanks for your reply. However, this is not what the Reference Guide says:
    A portable format annotation API exists in the org.springframework.format.annotation
    package. Use @NumberFormat to format java.lang.Number fields. Use @DateTimeFormat to format
    java.util.Date, java.util.Calendar, java.util.Long, or Joda Time fields.
    Perhaps the guide is wrong? Although I don't see why that annotation would be designed to only work with Joda objects. I am pretty sure that the Reference Guide is correct, and it is supposed to work with all listed types.

  6. #6
    Join Date
    Aug 2004
    Location
    London, UK
    Posts
    36

    Default

    Well, it might support java.util.Date, but JodaTime needs to be in the classpath anyway, that's my experience and it's also here:
    http://blog.springsource.com/2009/12...in-spring-3-0/
    Support for formatting Date, Calendar, and Joda Time fields with @DateTimeFormat, if Joda Time is on the classpath
    Ale Sarco

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

    Default

    Joda is in my classpath, and, in fact, I have tried those annotations on both Joda classes and java.util.Date objects. Never worked, and I couldn't figure out what was forcing the annotations to be ignored. I have a mix of Spring 3.0 and SWF 2.0.8, with a separate SWF conversion service. Not sure if that affects it or not. Don't have time to investigate it any further for now, since I reverted to the alternative way of formatting that works.

  8. #8

    Default Form

    Hi there,

    Thank you for all the replies.

    The problem I have is that I can't really use the Spring form tags because I am looping through a collection I have put in my model using the c:forEach tag.

    So I'm performing:

    Code:
    <c:forEach var="object" items="${objects}">
        ${object.annotedDate}
    </c:forEach>
    With each object being my annotated object.

    I would expect that this is would be a common requirement such as displaying items in a table so I may have to look at other methods?

    But I liked the fact that Spring would handle to Locale as indicated by Keiths blog.

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

    Default

    Why can't you combine the JSTL <c:forEach> with Spring's form tags? it is absolutely ok. You can access your model's collection element using the "forEach" tag's varStatus.count to calculate the iteration index. Something like this:

    Code:
    <form:form modelAttribute="yourModel" />
      ...
      <c:forEach var="item" items="${yourModel.yourList}" varStatus="counter">
          ...
           <form:input path="yourList[${counter.count-1}].yourDate" />
          ...
      </c:forEach>
    
    </form:form>
    You'd have to access your model object directly though, not through the "item" variable. Note that the varStatus.count is 1-based.

    If you don't need any bindings at all to be performed (if you are just displaying the data w/o the need to read it back upon the form submission, you can simply use JSTL's own formatting, the <fmt:formatDate> tag.)

    Quote Originally Posted by eggsy84 View Post
    Hi there,

    Thank you for all the replies.

    The problem I have is that I can't really use the Spring form tags because I am looping through a collection I have put in my model using the c:forEach tag.

    So I'm performing:

    Code:
    <c:forEach var="object" items="${objects}">
        ${object.annotedDate}
    </c:forEach>
    With each object being my annotated object.

    I would expect that this is would be a common requirement such as displaying items in a table so I may have to look at other methods?

    But I liked the fact that Spring would handle to Locale as indicated by Keiths blog.
    Last edited by constv; Jan 22nd, 2010 at 07:04 AM.

  10. #10

    Default thank you

    Hi there,

    Many thanks for your reply and good idea

    Unfortunately I have tried it and Spring seems to think:

    Code:
    Neither BindingResult nor plain target object for bean name 'objects[0]' available as request attribute
    Even though I have added the list of objects to the modelMap and confirmed that the list is populated.

    Unfortunately I cannot use the fmt tag because (please correct me if I'm wrong) it doesn't take into account the locale or does it?

    Eggsy

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
  •