SpringMVC数据转换

SpringMVC支持三种方式转换数据,分别是Converter,Formatter和基于注解方式。
1. Converter
可以将一种类型转为另外一种类型(如将日期转字符串)。若想创建 Converter,必须实现org.springframework.core.convert.converter.Converter这个接口。这个接口的声明如下:

public interface converter<S,T>//s为源类型,T为目标类型

1.1 实现Converter接口

/**
*@since 1.8+
*/
public class StringToLocaleDateConverter implements Converter<String,LocalDate> {//将String转为LocalDate类型
    private String datePattern;
    public StringToLocaleDateConverter(String datePattern){
        this.datePattern=datePattern;
    }
    public LocalDate convert(String  s) {
        try {
            return LocalDate.parse(s, DateTimeFormatter.ofPattern(datePattern));
        }catch(DateTimeParseException e){
            throw new IllegalArgumentException("invalid date format.Please use this pattern\""+datePattern+"\"");
        }
    }
}

1.2 spring-context.xml文件的编写

//配置conversionService
<mvc:annotation-driven conversion-service="conversionService"/>//必加
<bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean">
    <property name="converters">//应用程序中所使用的所有定制的Converter
        <list>
            <bean class="common.StringToLocaleDateConverter">
               <constructor-arg type="java.lang.String" value="yyyy-MM-dd"/>
            </bean>
        </list>
    </property>
</bean>

2. Formatter
将String类型转为另外一种类型,更适合Web程序。
创建Formatter,需要实现org.springframework.format.Formatter接口。接口的生命如下:

public interface Formatter<T>//T为转换后的目标类型

该接口有parse和print两个方法,其中parse用于数据转换,print用于数据格式化。

T parse(String text,java.util.Locale locale)
String print(T object,java.util.Locale locale)

2.1 实现Format接口

/**
*@since 1.8+
*/
 public class StringToLocalDateFormatter implements Formatter<LocalDate> {
    private String datePartten;
    private DateTimeFormatter formatter;
    public StringToLocalDateFormatter(String datePartten){
        this.datePartten=datePartten;
        formatter=DateTimeFormatter.ofPattern(datePartten);
    }
    public LocalDate parse(String text, Locale locale) throws ParseException {
        try{
            return LocalDate.parse(text,DateTimeFormatter.ofPattern(datePartten));
        }catch(DateTimeParseException e){
            throw new IllegalArgumentException("Invalid date format.Please user this pattern \""+datePartten+"\"");
        }
    }
    public String print(LocalDate object, Locale locale) {
        return object.format(formatter);
    }
}

2.2 spring-context.xml文件的编写

<context:component-scan base-package="common"/>//小心遗忘
<mvc:annotation-driven conversion-service="conversionService"/>
<bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
    <property name="formatters">
        <set>//这里是set不是list
            <bean class="common.StringToLocalDateFormatter">
                <constructor-arg type="java.lang.String" value="yyyy-MM-dd"/>
            </bean>
        </set>
    </property>
</bean>

1.3 基于注解
在编写JavaBean时,可以在属性前加上注解,而不用配置其他属性。

public class User{
@DateTimeFormat(pattern="yyyy-MM-dd")//可以将"1999-11-10"的字符串转到Date类型的birthday属性中
private Date birthday;
@NumberFormat(pattern="#,###.##")//可以将4,5000.00的字符串转换为long类型的salary属性中
private long salary;
/*Getter and Setter*/
}

总结:使用Converter和Formatter时,要实现相应的接口,同时要在spring配置文件中配置conversionService属性,其中Converter对应的加载类为org.springframework.context.support.ConversionServiceFactoryBean,而Formatter对应的类为org.springframework.format.support.FormattingConversionServiceFactoryBean。并设置相应的属性。而使用基于注解方式则简便许多。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Spring MVC中,数据转换是指将请求参数转换为制器方法的参数类型或将控制器方法的返回值转换为响应数据的过程。Spring MVC提供了多种数据转换的方式,包括基本类型转换、日期类型转换、集合类型转换等。 1. 基本类型转换:Spring MVC可以自动将请求参数转换为控制器方法的基本类型参数,如字符串转换为整数、浮点数等。 2. 自定义类型转换:如果需要将请求参数转换为自定义的Java对象,可以使用自定义类型转换器。通过实现Converter接口或使用注解@Converter来定义类型转换器,然后在配置文件中进行注册。 3. 格式化:Spring MVC支持使用注解@DateTimeFormat对日期类型进行格式化,将请求参数按照指定的格式转换为日期对象。 4. 集合类型转换:当控制器方法的参数或返回值是集合类型时,Spring MVC可以自动将请求参数转换为集合对象或将集合对象转换为响应数据。可以使用注解@InitBinder对集合类型进行配置。 5. JSON数据转换:Spring MVC内置了对JSON数据的支持,可以将请求参数或响应数据以JSON格式进行转换。可以使用注解@ResponseBody将方法的返回值直接转换为JSON格式的响应数据。 6. XML数据转换:除了JSON数据,Spring MVC还支持将请求参数或响应数据以XML格式进行转换。可以使用注解@RequestBody将请求参数转换为XML格式的对象,使用注解@ResponseBody将方法的返回值转换为XML格式的响应数据

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值