zoukankan      html  css  js  c++  java
  • springmvc——@InitBinder注解

    转自http://www.cnblogs.com/douJiangYouTiao888/p/6765220.html

    有些类型的数据是无法自动转换的,比如请求参数中包含时间类型的数据,无法自动映射到Controller里的Date参数。需要使用@initBinder注解为binder提供一个数据的转换器,这个转换器可以自己实现,也可以用spring官方的一些实现。比如:

    package com.wang.action;
    
    import java.text.SimpleDateFormat;
    import java.util.Date;
    import org.springframework.beans.propertyeditors.CustomDateEditor;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.WebDataBinder;
    import org.springframework.web.bind.annotation.InitBinder;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestParam;
    /**
     * 测试@initBinder注解
     * @author wlyfree
     */
    @Controller
    public class BinderAction {
    
        @RequestMapping("/sb2.do")
        public void doTest(@RequestParam(value="name")String name,@RequestParam(value="age")double age,@RequestParam(value="nowTime")Date nowTime){
            System.err.println("name:" + name);
            System.err.println("age:" + age);
            System.err.println("nowTime:" + nowTime);
        } 
        
        @InitBinder
        public void initBinder(WebDataBinder binder){
            binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), true));
        }
    }
    

      

  • 相关阅读:
    shell 函数
    使用Alpine镜像构建镜像
    macos修改vmware Fusion的NAT网络
    K8s Pod与宿主机时区不同步
    nginx热升级
    awk分析web日志
    k8s 新建用户远程连接集群和context切换
    查询出口公网ip
    微服务之服务网格 Istio
    Systemd 、systemctl进程管理工具
  • 原文地址:https://www.cnblogs.com/jlustone/p/7514780.html
Copyright © 2011-2022 走看看