天天看點

SSM——SpringMVC資料類型轉換SpringMVC資料類型轉換和格式化

文章目錄

  • SpringMVC資料類型轉換和格式化
    • (1)Converter
    • (2)Formatter
    • (3)@DateTimeFormat注解--日期格式轉換
    • 日期格式化标簽

SpringMVC資料類型轉換和格式化

資料轉換

:請求發送的參數一般是String類型,但是pojo的屬性資料類型不一定都是String類型,那麼就需要将String類型轉換為pojo屬性的資料類型。SpringMVC 在做參數綁定的時候,會自動的幫助我們進行資料類型的轉換。但是對于日期的類型(

日期有格式

),springMVC不會幫我們自動的轉換,另外有一些時候springMVC自動轉換的方式不能完全滿足我們的需求。

是以我們就需要資料轉換器:Converter和Fromatter

(1)Converter

将一種資料類型轉換為另一種資料類型。

  • 開發步驟:
  • (1)開發自定義的轉換器。

    要求:實作接口org.springframework.core.convert.converter.Converter;重寫convert方法。

    在Convert方法中編寫資料類型轉換的邏輯。

  • (2)自定義的轉換配置。

    在springMVC配置檔案中配置:

方式一:通過ref引用一個定義的bean

要求這個bean要麼在配置檔案通過,要麼使用spring注解@Component。

<mvc:annotation-driven conversion-service="conversionService"/>
<bean id="conversionService" 
class="org.springframework.context.support.ConversionServiceFactoryBean">
           <property name="converters">
               <set>
                    <ref bean="dataConverter"/>
                </set>
           </property>
       </bean>

           

方法二:在converter配置裡面直接定義一個bean

<mvc:annotation-driven conversion-service="conversionService"/>
    <bean id="conversionService" 
		class="org.springframework.context.support.ConversionServiceFactoryBean">
           <property name="converters">
               <set>
                   <bean class="com.ssm.converter.DateConverter"></bean>
                </set>
           </property>
       </bean>
           

(2)Formatter

将String類型轉化另外一種類型。

SpringMVC的資料轉換器:FormattingConversionServiceFactoryBean

開發步驟:

(1)自定義格式化器

要求:實作接口org.springframework.format.Formatter;重寫parse和print方法。

import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Date;import java.util.Locale;
import org.springframework.format.Formatter;
public class DateFormatter implements Formatter<Date>{
    
    private String datePattern;//日期格式字元串
    
    private SimpleDateFormat dateFormat;//日期格式類
    
    public DateFormatter(String datePattern) {
        this.datePattern = datePattern;
        dateFormat = new SimpleDateFormat(datePattern);
    }
           

//将Date格式化為指定日期字元串,傳回目标對象的字元串表示法

@Override
    public String print(Date date, Locale locale) {
        return dateFormat.format(date);
    }
           

//将字元串日期解析成Date對象

@Override
    public Date parse(String source, Locale locale) throws ParseException {
        
        return dateFormat.parse(source);
    }

}
           

(2)配置格式化器

在springMVC配置檔案中配置:

<bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
     <property name="formatters">
          <set>
             <bean class="com.ssm.converter.DateFormatter">
                 <constructor-arg name="datePattern" type="java.lang.String" value="yyyy-MM-dd"/>
             </bean>
          </set>
     </property></bean>
           

(3)@DateTimeFormat注解–日期格式轉換

SSM——SpringMVC資料類型轉換SpringMVC資料類型轉換和格式化

(1)springMVC預設不支援頁面上的日期字元串到背景的Date的轉換

有兩種方式

  • 第一種使用注解
public class Person {
    private int id;
    private String username;
    private String password;
    private String city;
    private Birthday birthday;
    @DateTimeFormat(pattern ="yyyy-MM-dd")
    private Date birthday2;
           

注解後面的格式怎麼寫?看請求發送的真實資料

SSM——SpringMVC資料類型轉換SpringMVC資料類型轉換和格式化
  • 第二種自己編寫 轉換類,配置到springMVC(了解)

    編寫自定義日期類型轉換器實作步驟:

1.編寫自定義轉換器實作Converter重寫方法,進行轉換

//1:将頁面上送出的日期字元串,轉成Date對象

public class DateTimeFormatConvert implements Converter<String, Date> {
    public Date convert(String s) {
        System.out.println("convert "+s);
        //2:轉換器
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        try {
            date = sdf.parse(s);//2020-10-14
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }
} 
           

2.springmvc.xml中配置轉換工廠,将我們的轉換器設定到converters集合中

<bean id="formattingConversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
        <property name="converters">
            <set>
                <bean id="dateTimeFormatConverter" class="com.wzx.util.DateTimeFormatConvert"></bean>
            </set>
        </property>
    </bean>

           

将轉換工廠對象挂載到處理器擴充卡上(挂載到注解驅動)

日期格式化标簽

  • (1)導入标簽
  • (2)調用日期格式化标簽