我正在将 JSTL 与 Spring 一起使用,并且我得到了代码:
<form:input type="datetime-local" path="startDate" />
其中 startDate 是 java.util.Date
如何从输入中获取日期和时间?是否有正确的方法或者我应该从输入中获取字符串并编写代码将其转换为 java.util.Date?
提前致谢。
这是我的 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>
这是我的 TareaForm.java,它是命令类:
包web.controller;
导入 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:
包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());
}
}
这是我的 dispatcher-servlet 中控制器的配置:
<bean class="web.controller.nuevaTareaController">
<property name="formView" value="nuevaTarea" />
<property name="successView" value="tareaCreada" />
<property name="gestorTareas" ref="tareas" />
</bean>