Announcement Announcement Module
Collapse
No announcement yet.
Parsing string to datetime in form custom view Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Parsing string to datetime in form custom view

    Hello everyone,

    I am stuck with a problem here. First off, i am a complete newbie, so i hope i can make myself clear and that this question does not come too obvious.

    I am trying to generate custom views for a roo project using tiles, jspx and custom controllers too. The project works over a postgres DB. The scaffold views generated by ROO work fine.

    In my custom view I have a form for new users requiring a date for the birthday, but when i submit the form i get this error on the browser, right below the birthday input:

    Failed to convert property value of type java.lang.String to required type java.util.Date for property birthDate; nested exception is org.springframework.core.convert.ConversionFailedE xception: Unable to convert value "2010-10-04" from type java.lang.String to type java.util.Date; nested exception is java.lang.IllegalArgumentException: Invalid format: "2010-10-04" is malformed at "10-10-04"

    Here is the line for the datetime input in my jspx file:
    HTML Code:
    <field:datetime field="birthDate" id="newUserForm_birthDate" required="true" dateTimePattern="${profile_birthdate_date_format}" label="Fecha de nacimiento" z=""/>
    and i copied the following java methods from the aj file to my java controller file:
    • public String create(@Valid Profile profile, BindingResult result, Model model)
    • void registerConverters(WebDataBinder binder) with the annotation @InitBinder
    • void addDateTimeFormatPatterns(Model model)
    • Converter<Profile, String> getProfileConverter()

    I copied other methods, but i thinks those are not related to this problem.

    I found a similiar problem in the WEB forum here, but i can not tell where the equivalent files are, the ones generated by ROO.

    I do not know what to try next. Can anyone point me in the right direction?
    Thanks.

  • #2
    Solution

    I found the solution to this problem,

    It was a detail and pretty obviuos, but well...i am learning.

    If someone might met the same problem:

    Do not forget to add the following line in the controller method that displays the form for the first time:

    Code:
    addDateTimeFormatPattern(model);
    This line adds a valid format to the date before you submit the form, so if it is missing the date wont be formatted by the time you save the form.

    Here is the code of the controller:

    Code:
    @RequestMapping(method = RequestMethod.GET)
        public String list(@RequestParam(value = "page", required = false) Integer page, @RequestParam(value = "size", required = false) Integer size, Model model) {
            if (page != null || size != null) {
                int sizeNo = size == null ? 10 : size.intValue();
                //model.addAttribute("profiles", Device.findDeviceEntries(page == null ? 0 : (page.intValue() - 1) * sizeNo, sizeNo));
                model.addAttribute("profiles", Profile.findProfileEntries(page == null ? 0 : (page.intValue() - 1) * sizeNo, sizeNo));
                //float nrOfPages = (float) Device.countDevices() / sizeNo;
                float nrOfPages = (float) Profile.countProfiles() / sizeNo;
                model.addAttribute("maxPages", (int) ((nrOfPages > (int) nrOfPages || nrOfPages == 0.0) ? nrOfPages + 1 : nrOfPages));
            } else {
                model.addAttribute("profiles", Profile.findAllProfiles());
            }
            model.addAttribute("profile", new Profile());
            addDateTimeFormatPatterns(model);
            return "personProfiles";
        }
    Happy programming everyone.

    Comment

    Working...
    X