提交Spring表单时日期格式错误 [英] Wrong date format when submit Spring form

查看:125
本文介绍了提交Spring表单时日期格式错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用Spring MVC和Thymeleaf的项目。
我需要根据每个用户的偏好以不同的格式显示日期。
例如,UserA要显示日期,例如MM / dd / yyyy,而UserB要显示日期,例如dd / MM / yyyy。



为此,我使用此thymeleaf参数:

  th:value = $ {#dates.format(myDate,dateFormat)} 

值 dateFormat基于用户首选项。



我的问题是日期输入是在表单中,而当我提交表单时,它的格式不正确。我总是得到MM / dd / yyyy。



如果我选择dd / MM / yyyy格式并输入18/01/2016,则在我的弹簧控制器中,我会得到 Thu Jun 01 00:00:00 CEST 2017,对应于2017年6月1日的dd / MM / yyyy。



我该怎么办才能与



这是我的代码:

  ; form th:action = @ {/ test} th:object = $ {filter} th:method = POST> 
<输入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筛选器筛选器,模型模型){

Systeme.out。 println(model.dateFormat);
// dd / MM / yyyy

Systeme.out.println(filter.myDate.toString());
// Thu Jun 01 00:00:00 CEST 2017

返回测试;
}


解决方案

我发现Spring读取了Web请求中发送的值,并尝试将其与Filter对象绑定。



对于日期值,除了查找值外格式为 MM / dd / yyyy。如果您以其他格式发送值,则该值无效。



要解决此问题,可以使用注释 InitBinder。

  @InitBinder 
private void dateBinder(WebDataBinderinder){
//要解析或输出您的日期
SimpleDateFormat dateFormat = new SimpleDateFormat(dateFormat());
//创建新的CustomDateEditor
CustomDateEditor编辑器= new CustomDateEditor(dateFormat,true);
//将其注册为Date类型的自定义编辑器
inder.registerCustomEditor(Date.class,editor);
}

此方法针对每个Web请求执行。在这里,我调用我的 dateFormat()方法来获取用户首选项中的格式,并向Spring说,它在Web请求中找到的所有java.util.Date都具有这种格式。 $ b

这是我的完整代码:



过滤器:

 导入java.util.Date; 

@lombok.Data
公共类TestDateFilter {
private Date myDate;

public TestDateFilter(){
this.myDate = new Date();
}
}

控制器:

  @RequestMapping(value = / testdate)
public String testDate(Model model){
model.addAttribute( filter,new TestDateFilter());
返回 testdate;
}

@RequestMapping(value = / testdate,method = RequestMethod.POST)
public String testDatePost(@ModelAttribute( filter)TestDateFilter过滤器,模型模型) {
System.out.printf(filter.getLoadingStartDate()。toString());
System.out.printf(dateFormat());
返回 testdate;
}

@ ModelAttribute( dateFormat)
public String dateFormat(){
return userPreferenceService.getDateFormat();
}

@InitBinder
private void dateBinder(WebDataBinderinder){
//用于解析或输出日期的日期格式
SimpleDateFormat dateFormat = new SimpleDateFormat(dateFormat());
//创建新的CustomDateEditor
CustomDateEditor编辑器= new CustomDateEditor(dateFormat,true);
//将其注册为Date类型的自定义编辑器
inder.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>
提交
< / button>
< / div>
< / div>
< / div>
< / form>


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.

To do this, I use this thymeleaf parameter:

th:value="${#dates.format(myDate, dateFormat)}"

The value "dateFormat" is based on the user preference. This works fine.

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.

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?

Here is my code:

<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>

Controller:

@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";
}

解决方案

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.

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.

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);
}

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.

Here is my full code :

Filter :

import java.util.Date;

@lombok.Data
public class TestDateFilter {
    private Date myDate;

    public TestDateFilter() {
        this.myDate = new Date();
    }
}

Controller :

@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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆