程序问答   发布时间:2022-06-01  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了提交Spring表单时日期格式错误大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决提交Spring表单时日期格式错误?

开发过程中遇到提交Spring表单时日期格式错误的问题如何解决?下面主要结合日常开发的经验,给出你关于提交Spring表单时日期格式错误的解决方法建议,希望对你解决提交Spring表单时日期格式错误有所启发或帮助;

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

对于日期值,除了找到格式为“ MM / dd / yyyy”的值外。如果您以其他格式发送值,则该值不起作用。

要解决此问题,可以使用注释“ 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);
}

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

这是我的完整代码:

筛选器:

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 MVC和Thymeleaf的项目。我需要根据每个用户的偏好以不同的格式显示日期。例如,用户A要显示日期,例如MM / dd /
yyyy,而用户B要显示日期,例如dd / MM / yyyy。

为此,我使用以下百里香参数:

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

值“ dateFormat”基于用户首选项。这很好。

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

如果我选择格式dd / MM / yyyy并输入18/01/2016,则在我的弹簧控制器中,我获得“ Thu Jun 01 00:00:00 CEST
2017”,对应于dd / MM / yyyy中的01/06/2017。 。

如何使用所需的格式显示日期?

这是我的代码:

<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表单时日期格式错误全部内容,希望文章能够帮你解决提交Spring表单时日期格式错误所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: