欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

spring mvc使用@InitBinder標(biāo)簽對表單數(shù)據(jù)綁定的方法

 更新時間:2018年03月14日 15:45:24   作者:lemrose  
這篇文章主要介紹了spring mvc使用@InitBinder標(biāo)簽對表單數(shù)據(jù)綁定的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

在SpringMVC中,bean中定義了Date,double等類型,如果沒有做任何處理的話,日期以及double都無法綁定。

解決的辦法就是使用spring mvc提供的@InitBinder標(biāo)簽

在我的項(xiàng)目中是在BaseController中增加方法initBinder,并使用注解@InitBinder標(biāo)注,那么spring mvc在綁定表單之前,都會先注冊這些編輯器,當(dāng)然你如果不嫌麻煩,你也可以單獨(dú)的寫在你的每一個controller中。剩下的控制器都繼承該類。spring自己提供了大量的實(shí)現(xiàn)類,諸如CustomDateEditor ,CustomBooleanEditor,CustomNumberEditor等許多,基本上夠用。

當(dāng)然,我們也可以不使用他自己自帶這些編輯器類,那下面我們自己去構(gòu)造幾個

import org.springframework.beans.propertyeditors.PropertiesEditor;  
public class DoubleEditor extends PropertiesEditor {  
  @Override  
  public void setAsText(String text) throws IllegalArgumentException {  
    if (text == null || text.equals("")) {  
      text = "0";  
    }  
    setValue(Double.parseDouble(text));  
  }  
  
  @Override  
  public String getAsText() {  
    return getValue().toString();  
  }  
}  
import org.springframework.beans.propertyeditors.PropertiesEditor; 
public class IntegerEditor extends PropertiesEditor {  
  @Override  
  public void setAsText(String text) throws IllegalArgumentException {  
    if (text == null || text.equals("")) {  
      text = "0";  
    }  
    setValue(Integer.parseInt(text));  
  }  
  
  @Override  
  public String getAsText() {  
    return getValue().toString();  
  }  
}  
import org.springframework.beans.propertyeditors.PropertiesEditor;  
public class FloatEditor extends PropertiesEditor {  
  @Override  
  public void setAsText(String text) throws IllegalArgumentException {  
    if (text == null || text.equals("")) {  
      text = "0";  
    }  
    setValue(Float.parseFloat(text));  
  }  
  
  @Override  
  public String getAsText() {  
    return getValue().toString();  
  }  
}  
import org.springframework.beans.propertyeditors.PropertiesEditor; 
public class LongEditor extends PropertiesEditor {  
  @Override  
  public void setAsText(String text) throws IllegalArgumentException {  
    if (text == null || text.equals("")) {  
      text = "0";  
    }  
    setValue(Long.parseLong(text));  
  }  
  
  @Override  
  public String getAsText() {  
    return getValue().toString();  
  }  
} 

在BaseController中

@InitBinder  
  protected void initBinder(WebDataBinder binder) {  
    binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"), true));  
/    binder.registerCustomEditor(int.class, new CustomNumberEditor(int.class, true));  
    binder.registerCustomEditor(int.class, new IntegerEditor());  
/    binder.registerCustomEditor(long.class, new CustomNumberEditor(long.class, true)); 
    binder.registerCustomEditor(long.class, new LongEditor());  
    binder.registerCustomEditor(double.class, new DoubleEditor());  
    binder.registerCustomEditor(float.class, new FloatEditor());  
  }  

復(fù)制代碼 代碼如下:

public class org.springframework.beans.propertyeditors.PropertiesEditor extends java.beans.PropertyEditorSupport { 

看到?jīng)]?如果你的編輯器類直接繼承PropertyEditorSupport也可以。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • MybatisPlus中的save方法詳解

    MybatisPlus中的save方法詳解

    save方法是Mybatis-plus框架提供的一個添加記錄的方法,它用于將一個實(shí)體對象插入到數(shù)據(jù)庫表中,這篇文章主要介紹了MybatisPlus中的save方法,需要的朋友可以參考下
    2023-11-11
  • SpringBoot中WEB的啟動流程分析

    SpringBoot中WEB的啟動流程分析

    今天我們就來分析下springboot啟動web項(xiàng)目整個流程,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧
    2022-03-03
  • 如何使用IDEA 搭建 SpringCloud 項(xiàng)目

    如何使用IDEA 搭建 SpringCloud 項(xiàng)目

    所謂微服務(wù),就是要把整個業(yè)務(wù)模塊拆分成多個各司其職的小模塊,做到單一職責(zé)原則,不會重復(fù)開發(fā)相同的業(yè)務(wù)代碼,實(shí)現(xiàn)真正意義上的高內(nèi)聚、低耦合,這篇文章主要介紹了如何使用IDEA 搭建 SpringCloud 項(xiàng)目,需要的朋友可以參考下
    2023-11-11
  • 分享令人目瞪口呆的?Java?代碼技巧

    分享令人目瞪口呆的?Java?代碼技巧

    這篇文章主要介紹了令人目瞪口呆的?Java?代碼技巧,本文從寫?Java?程序的小方面一直寫到大方面,來闡述了如何才能寫好?Java?程序,并告訴讀者們?nèi)绾尾拍芴岣咦陨淼木幋a水平,需要的朋友可以參考下
    2022-05-05
  • Hibernate用ThreadLocal模式(線程局部變量模式)管理Session

    Hibernate用ThreadLocal模式(線程局部變量模式)管理Session

    今天小編就為大家分享一篇關(guān)于Hibernate用ThreadLocal模式(線程局部變量模式)管理Session,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2019-03-03
  • Spring中Cache的使用方法詳解

    Spring中Cache的使用方法詳解

    這篇文章主要介紹了Spring中Cache的使用方法詳解,Spring Cache 是一個框架,實(shí)現(xiàn)了基于注解的緩存功能,只需要簡單地加一個注解,就能實(shí)現(xiàn)緩存功能,Spring Cache 提供了一層抽象,底層可以切換不同的緩存實(shí)現(xiàn),需要的朋友可以參考下
    2024-01-01
  • java遞歸實(shí)現(xiàn)樹形結(jié)構(gòu)數(shù)據(jù)完整案例

    java遞歸實(shí)現(xiàn)樹形結(jié)構(gòu)數(shù)據(jù)完整案例

    遞歸算法的代碼比較簡潔,可讀性較好;但是在實(shí)際的業(yè)務(wù)處理中會出現(xiàn)多次的重復(fù)調(diào)用,如果處理不好,很容易出現(xiàn)StackOverflowError報錯,這篇文章主要給大家介紹了關(guān)于java遞歸實(shí)現(xiàn)樹形結(jié)構(gòu)數(shù)據(jù)的相關(guān)資料,需要的朋友可以參考下
    2023-04-04
  • Spring?AOP簡介及統(tǒng)一處理

    Spring?AOP簡介及統(tǒng)一處理

    AOP面向切面編程,它是一種思想,它是對某一類事情的集中處理,本文給大家介紹Spring?AOP簡介及統(tǒng)一處理,感興趣的朋友跟隨小編一起看看吧
    2023-09-09
  • 淺析Java中線程的創(chuàng)建和啟動

    淺析Java中線程的創(chuàng)建和啟動

    這篇文章運(yùn)用實(shí)例代碼介紹了Java中線程的創(chuàng)建和啟動,非常詳細(xì),有需要的朋友們可以參考借鑒,下面一起來看看。
    2016-08-08
  • IDEA 使用 SpotBugs 找出你代碼中的bug問題

    IDEA 使用 SpotBugs 找出你代碼中的bug問題

    這篇文章主要介紹了IDEA 使用 SpotBugs 找出你代碼中的bug問題,重點(diǎn)給大家介紹SpotBugs 在 idea 中的安裝和使用,感興趣的朋友跟隨小編一起看看吧
    2021-07-07

最新評論