zoukankan      html  css  js  c++  java
  • spring mvc绑定参数之 类型转换 有三种方式:

    spring mvc绑定参数之类型转换有三种方式:

    1.实体类中加日期格式化注解(上次做项目使用的这种。简单,但有缺点,是一种局部的处理方式,只能在本实体类中使用。方法三是全局的。)

    @DateTimeFormat(pattern="yyyy-MM-dd HH:mm")
    private Date creationTime;

    2.属性编辑器(复杂)

    spring3.1之前

    在Controller类中通过@InitBinder完成

    /**

    • 在controller层中加入一段数据绑定代码
      @param webDataBinder
      */
      @InitBinder
      public void initBinder(WebDataBinder webDataBinder) throws Exception{
      SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm");
      simpleDateFormat.setLenient(false);
      webDataBinder.registerCustomEditor(Date.class , new CustomDateEditor(simpleDateFormat , true));
      }
      备注:自定义类型转换器必须实现PropertyEditor接口或者继承PropertyEditorSupport类
      写一个类 extends propertyEditorSupport(implements PropertyEditor){
      public void setAsText(String text){
      SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy -MM-dd hh:mm");
      Date date = simpleDateFormat.parse(text);
      this.setValue(date);
      }
      public String getAsTest(){
      Date date = (Date)this.getValue();
      return this.dateFormat.format(date);
      }
      }

    3. 类型转换器Converter

    全局类型转换

    参考前期课程代码

  • 相关阅读:
    bzoj1103[POI2007]大都市meg
    bzoj1098[POI2007]办公楼biu
    bzoj1102[POI2007]山峰和山谷Grz
    POI刷题记录
    语法-指针
    dp-最长公共子序列
    如何判断素数
    C++的map用法
    stl-优先队列
    C++和Java的stack语法
  • 原文地址:https://www.cnblogs.com/wqkeep/p/11299806.html
Copyright © 2011-2022 走看看