springMVC前台傳入字符串日期參數轉化


            項目中,對象屬性日期為Date類型,而前台傳入日期為字符串類型,使用到SpringMVC框架,需進行轉換。

              注意使用springMVC框架,前台傳入時間參數,需實現initBinder方法把時間字符串轉換為Date對象。實現了該方法,會自動完成封裝Date類型屬性對象。      

  @InitBinder
  public void initBinder(WebDataBinder binder) {
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
    dateFormat.setLenient(false);
    binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true)); //true:允許輸入空值,false:不能為空值
  }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM