Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring JSP表单日期输入字段_Spring_Jsp_Tomcat_Intellij Idea - Fatal编程技术网

Spring JSP表单日期输入字段

Spring JSP表单日期输入字段,spring,jsp,tomcat,intellij-idea,Spring,Jsp,Tomcat,Intellij Idea,我使用Intellij中的SpringWeb应用程序创建了一个包含大量字符串的基本输入表单。当仅使用字符串时,表单成功地保存到后端,因此我决定在模型中添加一个日期字段,并尝试修改到controller/jsp以在输入表单中接受该字段(并显示在记录列表中)。我在输入表单中遇到问题,无法获取值 实体: @Temporal(TemporalType.DATE) @DateTimeFormat(pattern="dd.MM.yyyy") private Date dueDate; public Dat

我使用Intellij中的SpringWeb应用程序创建了一个包含大量字符串的基本输入表单。当仅使用字符串时,表单成功地保存到后端,因此我决定在模型中添加一个日期字段,并尝试修改到controller/jsp以在输入表单中接受该字段(并显示在记录列表中)。我在输入表单中遇到问题,无法获取值

实体:

@Temporal(TemporalType.DATE)
@DateTimeFormat(pattern="dd.MM.yyyy")
private Date dueDate;

public Date getDueDate() {
    return dueDate;
}

public void setDueDate(Date dueDate) {
    this.dueDate = dueDate;
}
JSP(我假设这里的值应该是空的,因为我以一个空字段开始填写?)


“我的调试”显示到期日期:null,因此在发布时,表单中没有为“我的日期”字段发送任何内容。这意味着,如果存储库保存,则不会保存日期字段。

尝试使用spring输入,如:

<div class="control-group">
   <form:label cssClass="control-label" path="dueDate">Due Date:</form:label>
    <div class="controls">
      <form:input path="dueDate" class="date" />
    </div>
</div>

这是未使用的,您根本没有提供值。

您必须在控制器中注册一个InitBinder,以便spring将您的日期字符串转换为java.util.date对象,并在命令对象中设置它。在控制器中包括以下内容:

 @InitBinder
    public void initBinder(WebDataBinder binder) {
        SimpleDateFormat sdf = new SimpleDateFormat("MM-dd-yyyy");
        sdf.setLenient(true);
        binder.registerCustomEditor(Date.class, new CustomDateEditor(sdf, true));
    }
使用以下命令修改jsp:

<input type="text" path="dueDate" class= "date" name = "dueDate" value = "<fmt:formatDate value="${cForm.dueDate}" pattern="MM-dd-yyyy" />"/>

我使用了上述解决方案的组合,它似乎有效(持续存在并将值正确返回到屏幕)。我的jsp是:

   <div class="control-group">
        <form:label cssClass="control-label" path="dueDate">Due Date:</form:label>
        <div class="controls">
            <form:input path="dueDate" class="date" />
        </div>
    </div>

到期日:
我已经添加了上面描述的Initbinder


由于这是一个用于插入(而非更新)的输入表单,我不想在for(但!)中包含值。

此处不需要InitBinder: 首先修改jsp,如Rishav Basu所说:

因为你没有给你的输入任何价值

然后在实体中更改@DateTimeFormat,如下所示:

@DateTimeFormat(pattern = "MM-dd-yyyy")
弹簧将进行装订


当然,只有在“其他”视图中对日期使用相同的格式时,它才有效。如果没有,则需要InitBinder。

在模型类中,使用spring提供的DateTimeFormat注释

import org.springframework.format.annotation.DateTimeFormat;
import java.util.Date;

@DateTimeFormat(pattern = "MM-dd-yyyy")
private Date birthdate;
在jsp中,日期将自动绑定,不会出错

<form:input path="birthdate" cssClass="form-control" />


注意:日期格式必须是dd-mm-yy,即2017年10月27日。如果使用其他格式,则会导致异常

我不确定为什么到期日期可以是
null
,但输入元素的value属性包含引号,需要将其更改为单引号或转义。
@DateTimeFormat(pattern = "MM-dd-yyyy")
import org.springframework.format.annotation.DateTimeFormat;
import java.util.Date;

@DateTimeFormat(pattern = "MM-dd-yyyy")
private Date birthdate;
<form:input path="birthdate" cssClass="form-control" />