最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

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

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

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

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

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也可以。

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

相關(guān)文章

  • mybatis通過TypeHandler?list轉(zhuǎn)換string類型轉(zhuǎn)換方式

    mybatis通過TypeHandler?list轉(zhuǎn)換string類型轉(zhuǎn)換方式

    這篇文章主要介紹了mybatis通過TypeHandler?list轉(zhuǎn)換string類型轉(zhuǎn)換方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-07-07
  • Java 面試題基礎(chǔ)知識(shí)集錦

    Java 面試題基礎(chǔ)知識(shí)集錦

    本文主要介紹Java基礎(chǔ)面試題集錦,這里整理了面試java工程師的基礎(chǔ)知識(shí)題錦,有需要的小伙伴可以參考下
    2016-09-09
  • springmvc+spring+mybatis實(shí)現(xiàn)用戶登錄功能(上)

    springmvc+spring+mybatis實(shí)現(xiàn)用戶登錄功能(上)

    這篇文章主要為大家詳細(xì)介紹了springmvc+spring+mybatis實(shí)現(xiàn)用戶登錄功能,比較基礎(chǔ)的學(xué)習(xí)教程,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-07-07
  • Springboot實(shí)現(xiàn)前后端分離excel下載

    Springboot實(shí)現(xiàn)前后端分離excel下載

    這篇文章主要介紹了Springboot實(shí)現(xiàn)前后端分離excel下載,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-11-11
  • Java文件下載ZIP報(bào)錯(cuò):Out of Memory的問題排查

    Java文件下載ZIP報(bào)錯(cuò):Out of Memory的問題排查

    本文主要介紹了Java項(xiàng)目中下載大文件(超過2G的ZIP文件)時(shí)出現(xiàn)內(nèi)存溢出(OutOfMemory:JavaHeapSpace)的問題,具有一定的參考價(jià)值,感興趣的可以了解一下
    2025-01-01
  • Java中EnumMap的使用解析

    Java中EnumMap的使用解析

    這篇文章主要介紹了Java中EnumMap的使用解析,EnumMap?是一種特殊的?Map,它要求自身所有的鍵來自某個(gè)枚舉類型,EnumMap?的內(nèi)部可以作為一個(gè)數(shù)組來實(shí)現(xiàn),因此它們的性能非常好,你可以放心地用?EnumMap?來實(shí)現(xiàn)基于枚舉的查詢,需要的朋友可以參考下
    2023-11-11
  • SpringBoot整合消息隊(duì)列RabbitMQ

    SpringBoot整合消息隊(duì)列RabbitMQ

    SpringBoot整合RabbitMQ很容易,但是整合的目的是為了使用,那要使用RabbitMQ就要對(duì)其有一定的了解,不然容易整成一團(tuán)漿糊。因?yàn)檎f到底,SpringBoot只是在封裝RabbitMQ的API,讓其更容易使用而已,廢話不多說,讓我們一起整它
    2023-03-03
  • IntelliJ IDEA窗口組件具體操作方法

    IntelliJ IDEA窗口組件具體操作方法

    IDEA剛接觸不久,各種常用工具窗口找不到,不小心關(guān)掉不知道從哪里打開,今天小編給大家分享這個(gè)問題的解決方法,感興趣的朋友一起看看吧
    2021-09-09
  • Java+EasyExcel實(shí)現(xiàn)文件的導(dǎo)入導(dǎo)出

    Java+EasyExcel實(shí)現(xiàn)文件的導(dǎo)入導(dǎo)出

    在項(xiàng)目中我們常常需要Excel文件的導(dǎo)入與導(dǎo)出,手動(dòng)輸入相對(duì)有些繁瑣,所以本文教大家如何在Java中輕松導(dǎo)入與導(dǎo)出Excel文件,感興趣的可以學(xué)習(xí)一下
    2021-12-12
  • SpringBoot中的Condition包下常用條件依賴注解案例介紹

    SpringBoot中的Condition包下常用條件依賴注解案例介紹

    這篇文章主要介紹了SpringBoot中的Condition包下常用條件依賴注解案例,文章基于Java的相關(guān)資料展開主題詳細(xì)內(nèi)容,需要的小伙伴可以參考一下
    2022-04-04

最新評(píng)論

八宿县| 东乌珠穆沁旗| 满城县| 灵寿县| 游戏| 且末县| 吉木萨尔县| 舞钢市| 高碑店市| 福清市| 和平区| 南召县| 三穗县| 莱芜市| 舒兰市| 武定县| 库伦旗| 肥乡县| 胶南市| 宁南县| 宁国市| 南和县| 双城市| 漳平市| 基隆市| 大埔区| 澄江县| 泽库县| 澎湖县| 科尔| 清河县| 赞皇县| 从江县| 西乡县| 成都市| 冷水江市| 武义县| 阿克陶县| 常山县| 图片| 临朐县|