SSM——SpringMVC数据类型转换



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注解–日期格式转换

在这里插入图片描述
(1)springMVC默认不支持页面上的日期字符串到后台的Date的转换

有两种方式

  • 第一种使用注解
 出生日期<input type="date" name="birthday2"/><br/>
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;

注解后面的格式怎么写?看请求发送的真实数据
在这里插入图片描述

  • 第二种自己编写 转换类,配置到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>

将转换工厂对象挂载到处理器适配器上(挂载到注解驱动)

  <mvc:annotation-driven conversion-service="formattingConversionService"/>

日期格式化标签

  • (1)导入标签
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
  • (2)调用日期格式化标签
<fmt:formatDate value="${item.birthday2}" pattern="yyyy年MM月dd日"/>

猜你喜欢

转载自blog.csdn.net/qq_41209886/article/details/109077980