zoukankan      html  css  js  c++  java
  • SpringMVC在传递date型数据时的配置

    查阅了好多资料, 最后才发现原来只是添加一个方法就能解决的问题, 但是看了半天又没看明白, 只是知道这么写就能成功, 先记下来, 以后再研究吧, 在配置好springMVC的时候, 可以在前台的form表单传递一个实体类, 后台spring会自动将name传递来的属性自动装配给实体类的class, 但是有一点, 如果实体类中有一个属性是一个date型的, 就会报出400的错误, "客户端传递的参数中有语法错误", 最后在controller的那个控制类里面机上这样一段代码, 但不知道是什么意思

        @org.springframework.web.bind.annotation.InitBinder
        public void InitBinder(/*HttpServletRequest request, */ServletRequestDataBinder binder) {
            // 不要删除下行注释!!! 将来"yyyy-MM-dd"将配置到properties文件中
            // SimpleDateFormat dateFormat = new
            // SimpleDateFormat(getText("date.format", request.getLocale()));
            System.out.println("执行了InitBinder方法");
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            dateFormat.setLenient(false);
            binder.registerCustomEditor(Date.class, null, new CustomDateEditor(dateFormat, true));
        } 

    使用@InitBinder标签, spring在将参数传递到后台的controller的时候就可以自动转换成日起类型的格式了

  • 相关阅读:
    并查集
    博弈——Bash、Nim、Wythoff
    博弈——SG函数模板
    数据结构——二叉树
    数据结构——链式队列
    sonar-maven-plugin问题
    确立核心模型
    调度思路+EurekaServer获得当前机器的instanceid
    简易的RestClient代码
    Spring Cloud App(Service) Pom示例
  • 原文地址:https://www.cnblogs.com/wgbs25673578/p/5793064.html
Copyright © 2011-2022 走看看