Results 1 to 8 of 8

Thread: Spring Roo Date field

  1. #1
    Join Date
    Aug 2011
    Location
    Williamsport, PA
    Posts
    29

    Default Spring Roo Date field

    Hello everyone,

    I'm new to Roo and have been working on a little test project. I'm currently reading Spring Roo in Action (Early release version).

    What I've done is to take a working form and controller that were under Roo's controll, and did a "push-in" refactor so I could have control over the controller and the form.

    After doing this the form no longer saved. I narrowed this down to the date field on the form. If I disable the field, then all of the data is saved except for the disabled date field. I'm not seeing any error messages though I am sure that they are there somewhere. The data simply fails to save.

    I have the date defined in my Person entity.

    Code:
        @Temporal(TemporalType.TIME)
        @DateTimeFormat(style = "M-")
        private Date dateOfBirth;

    I have this in my create view:

    Code:
    <form:input path="dateOfBirth" id="c_domain_Person_dateOfBirth" dojoType="dijit.form.DateTextBox" />
    I feel like I am missing some sort of conversion from the string thats coming in from the UI to the entity. Is there something that I should have added to the controller since I refactored and now have complete control over the PersonController?

    My environment is as follows:

    Ubuntu Linux 11.10
    Spring STS 2.8
    Spring 3
    MariaDB 5.3.2
    Apache Tomcat 7.0.22
    Spring Roo 1.1.5

    Any help that could be provided would be greatly appreciated.

    Thanks,
    John

  2. #2
    Join Date
    Apr 2011
    Posts
    12

    Default

    Not sure, but I had similar problems while using web flow with roo.

    Actually the default converter tries to set a String into the Date field. I used this workaround to jump across the problem while trying to understand it and find a good solution: I created in my entity a setter with String parameter that manages String input, avoiding the ValueCoercionException that was raised during value binding.
    As this is a workaround to get a prototype app working, it's f**king stinky

    Code:
        public void setExpiresAt(String text) {
            System.err.println("Flow validation workaroud for Date format field expiresAt - start");
            try {
                if (text != null && !text.isEmpty()) {
                    StringToDate stringToDate = new StringToDate();
                    try {
                        expiresAt = (Date) stringToDate.convertSourceToTargetClass(text, Date.class);
                    } catch (InvalidFormatException e) {
                        System.err.println("Wrong format: manual input. Forcing Date format");
                        stringToDate.setPattern("dd-MMM-yyyy");
                        expiresAt = (Date) stringToDate.convertSourceToTargetClass(text, Date.class);
                    }
                } else {
                    expiresAt = null;
                }
            } catch (Exception e) {
                System.err.println("Flow validation workaroud for Date format field expiresAt - exception");
            }
        }

  3. #3
    Join Date
    Aug 2011
    Location
    Williamsport, PA
    Posts
    29

    Default Thanks!

    Thanks. I'll definitely give this a try when I get back home this afternoon. I'll let you know how it works out.

    Thanks again,
    John

  4. #4
    Join Date
    Aug 2011
    Location
    Williamsport, PA
    Posts
    29

    Default

    @Scoutme,

    It seems that there is another error that is occurring and I can't quite track it down. I added the code that you posted and I wanted to debug through my version of it and noticed that if I have the date field enabled and click "save", a trip to the server is never made as though the submit button is not working.. If I disable the date field control, then it works as expected. I'm sure I am doing something stupid here or I missed something silly.

    Thanks,
    John

  5. #5
    Join Date
    Apr 2011
    Posts
    12

    Default

    Do you mean you put a debug breakpoint at the beginning of the method you added and it's nevery reached? Are you using WebFlow or plain mvc ?

  6. #6
    Join Date
    Aug 2011
    Location
    Williamsport, PA
    Posts
    29

    Default It was a bit odd...

    It was a bit odd...but I had a several break points set in the controller so that I would be sure that I would get a break somewhere. But it never stopped on any of the break points. I finally stopped the tc server and closed STS down, and restarted it up. Now I can hit the break points without any issue.

    I'm using Spring Roo 1.1.5 which uses Spring MVC 3. I'm not using Webflow. In the controller (which was generated by Roo) there is a method that is as follows:
    Code:
    	void addDateTimeFormatPatterns(Model uiModel) {
            uiModel.addAttribute("client_dateofbirth_date_format", DateTimeFormat.patternForStyle("M-", LocaleContextHolder.getLocale()));
        }
    When this is executed I get the following error:

    Code:
    org.springframework.validation.BindingResult.client=org.springframework.validation.BeanPropertyBindingResult: 1 errors
    Field error in object 'client' on field 'dateOfBirth': rejected value [2011-06-24]; codes [typeMismatch.client.dateOfBirth,typeMismatch.dateOfBirth,typeMismatch.java.util.Date,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [client.dateOfBirth,dateOfBirth]; arguments []; default message [dateOfBirth]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' for property 'dateOfBirth'; nested exception is java.lang.IllegalArgumentException: Could not parse date: Unparseable date: "2011-06-24"]
    I have dateOfBirth defined this way in my entity:

    Code:
        @Temporal(TemporalType.DATE)
        @DateTimeFormat(style="M-")
        private Date dateOfBirth;
    I've tried setting the dateformatter using @InitBinder without any success. Any suggestions?

    Thanks,
    John

  7. #7
    Join Date
    Aug 2011
    Location
    Williamsport, PA
    Posts
    29

    Default I got it! [SOLVED]

    Ok, apparently, I had to setup an InitBinder in my controller, as such:


    Code:
    	@InitBinder
    	public void initBinder(WebDataBinder dataBinder)
    	{
    		dataBinder.setDisallowedFields(new String[] {"id"});
    		dataBinder.setRequiredFields(new String[] {"lastName","firstName"});
    		
    		dataBinder.registerCustomEditor(String.class, new StringTrimmerEditor(false));
    		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    		dateFormat.setLenient(false);
    		dataBinder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat,true));
    	}

    And then use the date format of "yyyy-MM-dd". Once I did this, it started working correctly.

    Thanks for your help scoutme!
    Last edited by howler9443; Oct 30th, 2011 at 09:07 PM.

  8. #8
    Join Date
    Apr 2011
    Posts
    12

    Default

    Just for my company ...

    Anyway, I'm gonna take your code and try to define a good practice to solve those problems in a good clean way - some docs might be useful for everybody here.

    Bye!

Posting Permissions

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