12

I have a simple POJO with a Date field. I want to bind the object with values from a form.
In the form I'm using jquery ui datepicker with date format("dd/mm/yyyy")
I have console.log the value and is a string : 13-11-2014
I'm using spring 4.0.7
I have on my dependencies joda-time 2.5

I get this exception:

Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' for property 'endDate'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type java.lang.String to type @org.springframework.format.annotation.DateTimeFormat java.util.Date for value '22-11-2014'; nested exception is java.lang.IllegalArgumentException: Invalid format: "22-11-2014" is malformed at "-11-2014"]

My POJO:

    package gr.gsis.announcement.model;


    import java.io.Serializable;
    import java.util.Date;

    import org.springframework.format.annotation.DateTimeFormat;

    public class Announcement implements Serializable{

        private static final long serialVersionUID = -1984554807132781312L;

        private int id;
        private String title;
        private String bodyText;

        @DateTimeFormat(pattern = "dd/MM/yyyy")
        private Date startDate;

        @DateTimeFormat(pattern = "dd/MM/yyyy")
        private Date endDate;

        private boolean activeFlag;

        public Announcement() {

        }

        public Announcement(int id, String title, Date startDate, Date endDate,
                boolean activeFlag) {
            this.id = id;
            this.title = title;
            this.startDate = startDate;
            this.endDate = endDate;
            this.activeFlag = activeFlag;
        }


        public int getId() {
            return id;
        }

        public void setId(int id) {
            this.id = id;
        }

        public String getTitle() {
            return title;
        }

        public void setTitle(String title) {
            this.title = title;
        }

        public String getBodyText() {
            return bodyText;
        }

        public void setBodyText(String bodyText) {
            this.bodyText = bodyText;
        }

        public Date getStartDate() {
            return startDate;
        }

        public void setStartDate(Date startDate) {
            this.startDate = startDate;
        }

        public Date getEndDate() {
            return endDate;
        }

        public void setEndDate(Date endDate) {
            this.endDate = endDate;
        }

        public boolean getActiveFlag() {
            return activeFlag;
        }

        public void setActiveFlag(boolean activeFlag) {
            this.activeFlag = activeFlag;
        }

        @Override
        public String toString() {
            return "Announcement [id=" + id + ", title=" + title + ", bodyText="
                    + bodyText + ", startDate=" + startDate + ", endDate="
                    + endDate + ", activeFlag=" + activeFlag + "]";
        }

My controller:

    package gr.gsis.announcement.controller;

    import gr.gsis.announcement.model.Announcement;

    import org.springframework.stereotype.Controller;
    import org.springframework.ui.Model;
    import org.springframework.web.bind.annotation.ModelAttribute;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;

    @Controller
    public class AnnouncementController {

        @RequestMapping("/")
        public String welcome(Model model) {
            model.addAttribute("greeting", "Welcome");

            return "welcome";

        }

        @RequestMapping(value="/create", method = RequestMethod.GET)
        public String getAnnouncementForm(Model model) {

            Announcement announcement = new Announcement();

            model.addAttribute("announcement", announcement);

            return "announcementForm";
        }

        @RequestMapping(value = "/create", method = RequestMethod.POST)
        public String processAnnouncement(@ModelAttribute("announcement") Announcement announcement) {

            System.out.println(announcement);

            return "announcementForm";
        }
    }

my Form JSP:

    <h2>Insert Announcement</h2>

        <form:form method="post" commandName="announcement">

            <div>
                <form:label path="title">Title</form:label>
                <form:input path="title" name="title" type="text"/>
            </div>

            <div>
                <form:label path="startDate">Start Date</form:label>
                <form:input path="startDate" cssClass="datepicker start"  name="startDate" type="text"/>
            </div>

            <div>
                <form:label path="endDate">End Date</form:label>
                <form:input path="endDate" cssClass="datepicker end" name="endDate" type="text"/>
            </div>

            <div>
                <form:label path="activeFlag">Active</form:label>
                <form:checkbox path="activeFlag" name="activeFlag"/>
            </div>


            <div>
                <input type="submit" class="button" value="Save" />
            </div>

        </form:form>
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.0/js/bootstrap.min.js"></script>
    <script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.11.2/jquery-ui.min.js"></script>
    <script>
        $(function() {
            $( ".datepicker" ).datepicker({ dateFormat: 'dd-mm-yy' });


        });

What am I doing wrong?

Thanks in advance

1 Answer 1

24

Your date format does not match input

@DateTimeFormat(pattern = "dd/MM/yyyy")

should be

@DateTimeFormat(pattern = "dd-MM-yyyy")
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.