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



我在Spring中使用JSTL,我得到了代码:

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

其中 startDate 是一个 java.util.Date

如何从输入中获取日期和时间?有没有正确的方法,或者我应该从输入中获取字符串并编写代码将其转换为java.util.Date?

提前谢谢。

这是我的新.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>

这是我的TareaForm.java它是命令类:

包网络控制器;

import java.util.Date;

公共类 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;
}

}

而且,这是我的控制器nuevaTareaController.java:

包网络控制器;

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());
  }
}

这是我的调度程序-servlet 中控制器的配置:

<bean class="web.controller.nuevaTareaController">
    <property name="formView" value="nuevaTarea" />
    <property name="successView" value="tareaCreada" />
    <property name="gestorTareas" ref="tareas" />
</bean>

我会使用属性编辑器从字符串转换为日期。 春天已经为你解决了这个问题。 最终结果应该是java.util.Date。

最新更新