简体   繁体   English

来自的java.util.Date <input type=“datetime-local” />

[英]java.util.Date from <input type=“datetime-local” />

I'm using JSTL with Spring and I've got the code: 我正在使用JSTL和Spring,我得到了代码:

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

where startDate is a java.util.Date 其中startDate是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? 有没有正确的方法或者我应该从输入中获取字符串并编写代码以将其转换为java.util.Date?

Thanks in advance. 提前致谢。

This is my nuevaTarea.jsp: 这是我的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: 这是我的TareaForm.java,它是命令类:

package web.controller; package web.controller;

import java.util.Date; import java.util.Date;

public class TareaForm { 公共课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: 而且,这是我的控制器nuevaTareaController.java:

package web.controller; 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: 这是我的dispatcher-servlet中控制器的配置:

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

I'd use a PropertyEditor to convert from String to Date. 我使用PropertyEditor将String转换为Date。 Spring has already taken care of this for you. Spring 已经为您解决了这个问题。 The end result should be a java.util.Date. 最终结果应该是java.util.Date。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM