zoukankan      html  css  js  c++  java
  • SpringMVC中向服务器传递时间参数时出现的问题

    1. 问题描述:

      今天在SpringMVC应用中上传参数的时候遇到如下问题:

    The request sent by the client was syntactically incorrect 

      这说明在提交的参数中,有的参数不符合服务器端数据要求。在排除其它参数没问题的情况下,确定是其中的时间参数除了问题。

      客户端传送的时间参数如下所示:

      

      而接受此参数的字段是一个Date类型的数据:

      

      在Controller中使用自动装箱:

      

      原因所在: 在装箱过程中时间的参数的转化出错。

    2. 问题解决

      2.1 方法一:

        在需要转变时间的Controller中添加如下代码:

    @InitBinder
    protected void initBinder(HttpServletRequest request,
                ServletRequestDataBinder binder) throws Exception {
        // DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        CustomDateEditor dateEditor = new CustomDateEditor(format, true);
        binder.registerCustomEditor(Date.class, dateEditor);
        super.initBinder(request, binder);
    }

      2.2 方法二:

        在实体类中对应的时间字段上加上如下注释:

    @DateTimeFormat(pattern="yyyy-MM-dd")
    private Date birthday;

        用这种方法的时候注意要引入joda-time.jar包,在Maven工程的pom.xml文件中加入如下依赖:

        

            <dependency>
                <groupId>joda-time</groupId>
                <artifactId>joda-time</artifactId>
                <version>2.9.4</version>
            </dependency>
  • 相关阅读:
    WPF中的句柄
    WPF中的焦点问题
    Vue项目(一):VSCode环境开发Vue程序以及其中遇到的问题
    C#实现三种方式的模拟按键
    c++温故之结构体写法
    WPF搜索框
    vue框架学习
    Git连接失败问题解决方案
    单击双击冲突解决 小程序
    uniapp 微信小程序 wx.createAnimation 实现向上滚动弹幕
  • 原文地址:https://www.cnblogs.com/linux-wangkun/p/5691701.html
Copyright © 2011-2022 走看看