SpringMVC之轉換器與格式化

SpringMVC框架:格式化與轉換器

   Converter(格式化)、Formatter(轉換器)

一、Converter類

2、Converter:任何層都可以、Formatter:專門爲Web層設計


3.Spring的Converter是將一種類型轉換成爲另一種類型的一個對象。創建Converter類爲一下步驟:

 1.org.springframework.core.convert.converter.Converter

 這個接口的聲明:

4.public interface Converter<S,T> -> S爲源類型,T爲目標類型

 例:Long -> Double類型

 public classname implements Converter<Long,Date>{}

 實現方法:

    T convert(S source) 方法是傳入一個S 的源類型

 %重點%

 1、利用bean 的構造器方法進行可以配置注入

   編寫bean -> org.springframework.context.support.conversion.ServicefactoryBean;

   <properties name = "converters">

      <list><bean class = "自定義類名全路徑"/></list>

   </properties>

 2、定義配置一個

   <mvc:anntation-driven conversion-service = “對應的beanid”>

 3.其他注意事項:

   BindResulting與getFieldError()方法的使用  

二、Formatter類

 1、限制Source-String,必須爲:源類型:只可以爲String,適用於Web層次結構

 2、例子:轉換爲類型:

   public interface Formatter<T>

   org.springfamework.format.Formatter

 3、方法:parse()、print()

 4.在Spring 的配置文件中配置一個FactoryBean 具體爲什麼不多說了,剩下的和Converter類差不多此外要說明的是Formatter-formatterRegister 實現 registerFormatters 方法 - addFormatter(類)



 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章