提交 Spring 表单时日期格式错误 [英] Wrong date format when submit Spring form
问题描述
我有一个使用 Spring MVC 和 Thymeleaf 的项目.我需要根据每个用户的喜好以不同的格式显示日期.例如,UserA 想显示像 MM/dd/yyyy 这样的日期,而 UserB 要显示像 dd/MM/yyyy 这样的日期.
I have a project where I use Spring MVC and Thymeleaf. I need to display dates with a different format for each user based on his preferences. For exemple, UserA want to display dates like MM/dd/yyyy and UserB want to display dates like dd/MM/yyyy.
为此,我使用了这个 thymeleaf 参数:
To do this, I use this thymeleaf parameter:
th:value="${#dates.format(myDate, dateFormat)}"
dateFormat"值取决于用户偏好.这很好用.
The value "dateFormat" is based on the user preference. This works fine.
我的问题是日期输入在表单中,当我提交表单时,它没有采用好的格式.我总是得到 MM/dd/yyyy.
My problem is that the date input is in a form, and when I submit the form, it doesn't take the good format. I always get MM/dd/yyyy.
如果我选择 dd/MM/yyyy 格式并输入 18/01/2016,在我的 spring 控制器中,我会获得Thu Jun 01 00:00:00 CEST 2017",它对应于 dd/中的 01/06/2017月/年.
If I choose the format dd/MM/yyyy and enter 18/01/2016, in my spring controller I obtain "Thu Jun 01 00:00:00 CEST 2017" which correspond to 01/06/2017 in dd/MM/yyyy.
我可以做些什么来获得我想要的格式的日期?
What can I do to have the date with the format that I want?
这是我的代码:
<form th:action="@{/test}" th:object="${filter}" th:method="POST">
<input type="date" th:type="date" class="form-control" th:id="myDate"
th:name="myDate" th:value="${#dates.format(filter.myDate, dateFormat)}"/>
</form>
控制器:
@RequestMapping(value = "/test", method = RequestMethod.POST)
public String myTest(@ModelAttribute Filter filter, Model model) {
Systeme.out.println(model.dateFormat);
// dd/MM/yyyy
Systeme.out.println(filter.myDate.toString());
// Thu Jun 01 00:00:00 CEST 2017
return "test";
}
推荐答案
经过一天的研究,发现Spring读取了web请求中发送的值,并尝试将其与Filter对象绑定.
After a day of research, I found that Spring read the value that is sent in the web request and try to bind it with the Filter object.
对于日期值,它会查找具有MM/dd/yyyy"格式的值.如果您发送具有其他格式的值,则它不起作用.
For a date value, it excepts to find a value with the "MM/dd/yyyy" format. If you send a value with another format, it doesn't work.
要解决这个问题,可以使用注解InitBinder".
To solve this problem, you can use the annotation "InitBinder".
@InitBinder
private void dateBinder(WebDataBinder binder) {
//The date format to parse or output your dates
SimpleDateFormat dateFormat = new SimpleDateFormat(dateFormat());
//Create a new CustomDateEditor
CustomDateEditor editor = new CustomDateEditor(dateFormat, true);
//Register it as custom editor for the Date type
binder.registerCustomEditor(Date.class, editor);
}
这个方法针对每个网络请求执行.在这里,我调用我的dateFormat()"方法来获取用户偏好中的格式,并向 Spring 说它在 Web 请求中找到的所有 java.util.Date 都具有这种格式.
This method is executed for each web request. Here I call my "dateFormat()" method to get the format in the user preference and say to Spring that all the java.util.Date that it find in web requests have this format.
这是我的完整代码:
过滤器:
import java.util.Date;
@lombok.Data
public class TestDateFilter {
private Date myDate;
public TestDateFilter() {
this.myDate = new Date();
}
}
控制器:
@RequestMapping(value = "/testdate")
public String testDate(Model model) {
model.addAttribute("filter", new TestDateFilter());
return "testdate";
}
@RequestMapping(value = "/testdate", method = RequestMethod.POST)
public String testDatePost(@ModelAttribute("filter") TestDateFilter filter, Model model) {
System.out.printf(filter.getLoadingStartDate().toString());
System.out.printf(dateFormat());
return "testdate";
}
@ModelAttribute("dateFormat")
public String dateFormat() {
return userPreferenceService.getDateFormat();
}
@InitBinder
private void dateBinder(WebDataBinder binder) {
//The date format to parse or output your dates
SimpleDateFormat dateFormat = new SimpleDateFormat(dateFormat());
//Create a new CustomDateEditor
CustomDateEditor editor = new CustomDateEditor(dateFormat, true);
//Register it as custom editor for the Date type
binder.registerCustomEditor(Date.class, editor);
}
HTML:
<form th:action="@{/testdate}" th:object="${filter}" th:method="POST">
<div class="row">
<div class="col-xs-12 col-sm-6">
<div class="input-group date">
<input type="date" th:type="date" class="form-control"
th:id="loadingStartDate" th:name="loadingStartDate"
th:value="${#dates.format(filter.loadingStartDate, dateFormat)}" />
</div>
</div>
</div>
<div class="row">
<div class="form-group">
<div class="col-xs-12 col-sm-12">
<button type="submit" class="btn btn-primary btn-lg pull-right">
submit
</button>
</div>
</div>
</div>
</form>
这篇关于提交 Spring 表单时日期格式错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!