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

  • 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, 03:16 PM.

  • #2
    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, 11:06 AM. Reason: paragraph not required

    Comment


    • #3
      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.)

      Comment


      • #4
        @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

        Comment


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

          Comment


          • #6
            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

            Comment


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

              Comment


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

                Comment


                • #9
                  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.)

                  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, 07:04 AM.

                  Comment


                  • #10
                    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

                    Comment


                    • #11
                      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

                      Comment


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

                        Comment


                        • #13
                          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

                          Comment


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

                            Comment


                            • #15
                              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

                              Comment

                              Working...
                              X