数据类型转换

1. 自定义类型转换器实现Converter<S,T>接口并加入到SpringMVC的IOC容器中:

            @Component
            public class EmployeeConverter implements Converter<String, Employee>{
                @Override
                public Employee convert(String source) {    
                    System.out.println(source);
                    if(source != null){
                        String[] vals = source.split("-");
                        if(vals != null && vals.length ==5){
                            String name = vals[0];
                            String email = vals[1];
                            Integer gender = Integer.parseInt(vals[2]);
                            Department department = new Department();
                            department.setId(Integer.parseInt(vals[3]));            
                            Employee employee = new Employee(null, name, email, gender, department);
                            return employee;
                        }
                    }
                    return null;
                }
            }

2.配置自定义转换器到FormattingConversionServiceFactoryBean工厂中!

<!-- 将ConversionService再作为annotation-driven的一个属性存在! -->
<mvc:annotation-driven conversion-service="conversionService"></mvc:annotation-driven>

<!-- 配置ConversionService -->
<bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
<property name="converters">
<set>
<ref bean="employeeConverter"/>
</set>
</property>
</bean>