- ConversionService是Spring类型转换体系核心借口。
- 可以利用ConversionserviceFactoryBean在Spring的容器中定义一个ConversionService。Spring将自动识别出IOC容器中的ConversionService,并在Bean属性配置及SpringMVC处理方法入参绑定等场合使用它进行数据的转换
- 可通过ConversionServiceFactoryBean的converters属性注册自定义的类型转换器
自定义类型转换器
如果我们想通过一个字符串转换为一个对象类型如
Bruce-1137@qq.com-0-101 -->
Employee
1. 前端页面
<form action="${pageContext.request.contextPath}/testConversionServiceConverter" method="post">
<input type="text" name="employee"/>
<button type="submit">提交</button>
</form>
2. 处理器
@Controller
public class TestHandler {
@Autowired
private EmployeeDao employeeDao;
@RequestMapping("/testConversionServiceConverter")
public String testConverter(@RequestParam("employee") Employee employee){
System.out.println("进来了"+employee);
employeeDao.save(employee);
return "redirect:/list";
}
}
3. 自定义转换器
@Component //实现接口:org.springframework.core.convert.converter.Converter;
public class EmployeeConverter implements Converter<String,Employee> {
// employeeBruce-1137@qq.com-0-101
@Override
public Employee convert(String source) {
if (source!=null){
String[] vals = source.split("-");
if (vals!=null && vals.length==4){
Employee employee = new Employee();
employee.setEmpName(vals[0]);
employee.setEmpEmail(vals[1]);
employee.setEmpGender(Integer.parseInt(vals[2]));
employee.setDepartment(new Department(Integer.parseInt(vals[3])));
return employee;
}
}
return null;
}
}
4. springmvc.xml配置
<!--1. 配置mvc标签的annotation-driven
2. 配置ConversionService
-->
<mvc:annotation-driven conversion-service="conversionService"></mvc:annotation-driven>
<!-- 配置ConversionService -->
<bean id="conversionService"
class="org.springframework.context.support.ConversionServiceFactoryBean">
<property name="converters">
<set>
<ref bean="employeeConverter"/>
</set>
</property>
</bean>
数值类型转换/日期类型格式化
前端表单
birth:<form:input path="empBirth"/><br>
salary:<form:input path="empSalary"/><br>
对应属性
@DateTimeFormat(pattern="yyyy-MM-dd")
private Date empBirth;
@NumberFormat(pattern="#,##,###.#")//#代表一个数字,‘,’会被忽略,‘.’不会
private Float empSalary;
springmvc.xml配置<mvc:annotation-driven >
<mvc:annotation-driven />
补充:
我们知道
<mvc:annotation-driven />
默认创建的conversionService实例为FormattingConversionServiceFactoryBean,且该bean内部已经注册了
- NumberFormatAnnotationFormatterFactory //Number类型转换
- JodaDateTimeFormatAnnotationFormatterFactory //Date日期格式化
如果我们既要自定义类型转换器,又要实现日期的格式化的话,那么便会冲突,因为
<mvc:annotation-driven conversion-service="conversionService"/>
<bean id="conversionService"
class="org.springframework.context.support.ConversionServiceFactoryBean">
<property name="converters">
<set>
<ref bean="employeeConverter"/>
</set>
</property>
</bean>
该conversionService对应是ConversionServiceFactoryBean,该Bean没有日期格式化和类型转换的能力;于是我们可以把该bean换成FormattingConversionServiceFactoryBean,这样鱼和熊掌就可以兼得了
<mvc:annotation-driven conversion-service="conversionService"/>
<!-- 配置ConversionService -->
<bean id="conversionService"
class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
<property name="converters">
<set>
<ref bean="employeeConverter"/>
</set>
</property>
</bean>
数据绑定出错,日期格式化出错提示
当我们绑定的数据出错,类型不配置,参数对不上;或日期格式化出错时,怎么才能得到错误信息呢?
只要在处理方法上加上出错参数就可以了
/**添加Employee*/
@RequestMapping(value="/emp",method=RequestMethod.POST)
public String save(Employee employee,BindingResult result){
if (result.getFieldErrorCount()>0){
for (FieldError error:result.getFieldErrors()){
System.out.println(error.getField()+":"+error.getDefaultMessage());
}
return "redirect:list";
}
employeeDao.save(employee);
return "redirect:list";
}