spring JSP 表单日期输入字段
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22787790/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JSP form date input field
提问by kayakpim
I've created a basic input form with a number of strings using Spring Web application in Intellij. The form successfully saves to the backend when using strings only so I decided to add a date field in the model and tried to modify to controller/jsp to accept this in the input form (and display in the record list). I'm having problems with the input form not getting the value.
我使用 Intellij 中的 Spring Web 应用程序创建了一个包含多个字符串的基本输入表单。当仅使用字符串时,表单成功保存到后端,因此我决定在模型中添加一个日期字段并尝试修改为控制器/jsp 以在输入表单中接受它(并显示在记录列表中)。我在输入表单无法获取值时遇到问题。
Entity:
实体:
@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 (I assume value should be blank here as I am starting with an empty field to fill in?):
JSP(我假设这里的值应该为空,因为我从一个空字段开始填写?):
<div class="control-group">
<form:label cssClass="control-label" path="dueDate">Due Date:</form:label>
<div class="controls">
<input type="text" path="dueDate" class= "date" name = "dueDate" value = "<fmt:formatDate value="" pattern="MM-dd-yyyy" />"/>
</div>
</div>
Controller:
控制器:
@RequestMapping(value = "/todos/add", method = RequestMethod.POST)
public String addUser(@ModelAttribute("todo") Todo todo, BindingResult result) {
System.err.println("Title:"+todo.getTitle());
System.err.println("Due Date:"+todo.getDueDate());
todoRepository.save(todo);
return "redirect:/todos/";
}
My debug shows Due Date:null so nothing is being send for my date field from the form when posted. This means that the date field is never saved then the repository save occurs.
我的调试显示截止日期:null,因此发布时表单中没有为我的日期字段发送任何内容。这意味着永远不会保存日期字段,然后会发生存储库保存。
回答by Rishav Basu
You have to register an InitBinder in your controller to let spring convert your date string to java.util.Date object and set it in command object. Include following in your controller :
您必须在您的控制器中注册一个 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));
}
Modify your jsp with :
使用以下命令修改您的 jsp:
<input type="text" path="dueDate" class= "date" name = "dueDate" value = "<fmt:formatDate value="${cForm.dueDate}" pattern="MM-dd-yyyy" />"/>
回答by Dipen
In your model class use the annotation provided by DateTimeFormat by spring
在您的模型类中,使用 spring 提供的 DateTimeFormat 注释
import org.springframework.format.annotation.DateTimeFormat;
import java.util.Date;
@DateTimeFormat(pattern = "MM-dd-yyyy")
private Date birthdate;
In your jsp the date will bind automatically without error
在您的 jsp 中,日期将自动绑定而不会出错
<form:input path="birthdate" cssClass="form-control" />
Note: date format must be dd-mm-yy i.e. 27-10-2017 if you use other format it will cause exception
注意:日期格式必须是 dd-mm-yy 即 27-10-2017 如果使用其他格式会导致异常
回答by Abhishek Nayak
try using spring input like:
尝试使用弹簧输入,如:
<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>
and in dueDate
input you have:
在dueDate
输入中你有:
value="<fmt:formatDate value="" pattern="MM-dd-yyyy" />"
this is unused, you have not provided value at all.
这是未使用的,您根本没有提供价值。
回答by kayakpim
I've used a combination of the solutions above and it seems to work (persists and returns the value correctly to the screen). My jsp is:
我使用了上述解决方案的组合,它似乎有效(坚持并将值正确返回到屏幕)。我的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>
And I've added the Initbinder described above.
我已经添加了上面描述的 Initbinder。
As this is an input form for insert (not update) I don't want to include a value on the for (yet!).
因为这是插入(不是更新)的输入表单,所以我不想在 for(还!)中包含一个值。
回答by Julien B
You don't need an InitBinder here : First modify you jsp like Rishav Basu said :
您在这里不需要 InitBinder :首先像 Rishav Basu 所说的那样修改您的 jsp:
<input type="text" path="dueDate" class= "date" name = "dueDate" value = "<fmt:formatDate value="${cForm.dueDate}" pattern="MM-dd-yyyy" />"/>
<input type="text" path="dueDate" class= "date" name = "dueDate" value = "<fmt:formatDate value="${cForm.dueDate}" pattern="MM-dd-yyyy" />"/>
because you didn't give any value into your input.
因为您没有为您的输入提供任何价值。
Then change your @DateTimeFormat in your entity like this :
然后在您的实体中更改您的 @DateTimeFormat ,如下所示:
@DateTimeFormat(pattern = "MM-dd-yyyy")
Spring will do the binding.
Spring 将进行绑定。
Of course, it works only if you use the same format for your date in the others views. If not, the InitBinder is required.
当然,只有当您在其他视图中为您的日期使用相同的格式时,它才有效。如果没有,则需要 InitBinder。