How to pass LocalDateTime from jsp to controller using java8 api(LocalDateTime)?

I need your help. Supossing i have form:form in my JSP, and inside form:form is form:input with date.. as below:

    <form:form modelAttribute="personalTask" method="POST" action="${pageContext.request.contextPath}/taskmgr/add">
    (...)
            <div class="input-group">
                    <form:input path="startDate" type="date" id="startDateTimePicker" name="search_message[displayDateFrom]" placeholder="YYYY-MM-DD hh:mm:ss" class="input-sm form-control"></form:input>
                    <div class="input-group-addon">
                            <span class="glyphicon glyphicon-calendar"> </span>
                    </div>
            </div>  
    (...)
    </form:form>

In controller I have method:

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public String addProfileTask(@ModelAttribute("personalTask")PersonalTask task, Principal principal){

            return "taskManager";
    }

PersonalTask looks like:

    public class PersonalTask{
    (...)
            @Column(name = "START_DATE", columnDefinition="TIMESTAMP", nullable = false)
            private LocalDateTime startDate;
    (...)
            public LocalDateTime getStartDate() {
                    return startDate;
            }
            public void setStartDate(LocalDateTime startDate) {
                    this.startDate = startDate;
            }
    (...)
    }

The question is... How to pass datetime from JSP and bind with @ModelAttribute("personalTask")PersonalTask task??

Upvotes: 1

Views: 2803

Answers (2)

fujy
fujy

Reputation: 5264

Use Spring Converters, spring already has a builtin converter @DateTimeFormat for Date and Time data types, from the documentation, it already supports java.time.LocalDateTime

Supports formatting by style pattern, ISO date time pattern, or custom format pattern string. Can be applied to java.util.Date, java.util.Calendar, java.lang.Long, Joda-Time value types; and as of Spring 4 and JDK 8, to JSR-310 java.time types too.

Example:

public class PersonalTask{
    @DateTimeFormat(pattern="dd/MM/yyyy")  // or any pattern you prefer 
    @Column(name = "START_DATE", columnDefinition="TIMESTAMP", nullable = false)
    private LocalDateTime startDate;
}

Upvotes: 2

Rocky
Rocky

Reputation: 132

A new LocalDateTime object can be created using a formatted string:

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime dateTime = LocalDateTime.parse(dateInitializer, formatter);

Use the date aquired from your JSP form to instantiate a new LocalDateTime object and pass that to your PersonTask object

To Bind the string from the date input to a field in PersonTask and use it to instantiate the LocalDateTime object:

public class PersonalTask{
(...)
        private String dateInitializer;

        @Column(name = "START_DATE", columnDefinition="TIMESTAMP", nullable = false)
        private LocalDateTime startDate;

        public PersonalTask(){
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            startTime = LocalDateTime.parse(dateInitializer, formatter);
        }
(...)
        public LocalDateTime getStartDate() {
                return startDate;
        }
        public void setStartDate(LocalDateTime startDate) {
                this.startDate = startDate;
        }
(...)
}

In JSP:

<form:input path="dateInitializer" type="date" id="startDateTimePicker" name="search_message[displayDateFrom]" placeholder="YYYY-MM-DD hh:mm:ss" class="input-sm form-control"></form:input>

Upvotes: 0

Related Questions