Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.util.Date from <input type="datetime-local" />

I'm using JSTL with Spring and I've got the code:

<form:input type="datetime-local" path="startDate" />

where startDate is a java.util.Date

How can I get the date and time from the input? Is there a right way or should I get the string from the input and write code to convert it to java.util.Date?

Thanks in advance.

This is my nuevaTarea.jsp:

<%@ include file="/WEB-INF/jsp/include.jsp" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Nueva tarea</title>
    <link rel="stylesheet" type="text/css" href="resources/css/style.css" media="screen" />
</head>
<body>
    <h1>Nueva tarea</h1>       
    <form:form action="nuevatarea.htm" method="POST" commandName="tareaForm"> 
        <label>Fecha de inicio:</label><form:input type="datetime-local" path="fechaInicio" /><br />
        <label>Fecha de fin:</label><form:input type="datetime-local" path="fechaFin" /><br />

        <input type="submit" value="Crear tarea" />
    </form:form>
</body>
</html>

This is my TareaForm.java which is the command class:

package web.controller;

import java.util.Date;

public class TareaForm {

private Date fechaInicio;
private Date fechaFin;

public Date getFechaFin() {
    return fechaFin;
}

public Date getFechaInicio() {
    return fechaInicio;
}

public void setFechaFin(Date fechaFin) {
    this.fechaFin = fechaFin;
}

public void setFechaInicio(Date fechaInicio) {
    this.fechaInicio = fechaInicio;
}

}

And, this is my controller nuevaTareaController.java:

package web.controller;

import javax.servlet.http.HttpServletRequest;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

public class nuevaTareaController extends SimpleFormController {

public nuevaTareaController() {
    setCommandClass(TareaForm.class);
    setCommandName("tareaForm");
}

    @Override
    protected Object formBackingObject(HttpServletRequest request) throws Exception {
    return (TareaForm)super.formBackingObject(request);
}   

   @Override
   protected ModelAndView onSubmit(Object command, BindException bindException)
    throws Exception {

    // Do something with (TareaForm)command
    return new ModelAndView(getSuccessView());
  }

}

This is the config of the controller in my dispatcher-servlet:

<bean class="web.controller.nuevaTareaController">
    <property name="formView" value="nuevaTarea" />
    <property name="successView" value="tareaCreada" />
    <property name="gestorTareas" ref="tareas" />
</bean>
like image 732
Jonás Avatar asked Sep 24 '12 20:09

Jonás


1 Answers

I'd use a PropertyEditor to convert from String to Date. Spring has already taken care of this for you. The end result should be a java.util.Date.

like image 64
duffymo Avatar answered Oct 07 '22 05:10

duffymo