在SpringMvc中會將來自web頁面的請求和響應數據與controller中對應的處理方法的入參進行綁定,即數據綁定。流程如下:
-1.SpringMvc主框架將ServletRequest對象及目標方法的入參實例傳遞給WebDataBinderFactory實例,以創建DataBinder實例對象
-2.DataBinder對象調用裝配在SpringMvc上下文中的ConversionService組件進行數據類型轉換,數據格式化工作,將Servlet中的請求信息填充到入參對象中。
-3.調用Validator組件對已經綁定了請求消息的入參對象進行數據合法性校驗,並最終生成數據綁定結果BindingData對象
-4.SpringMvc抽取BindingResult中的入參對象和校驗錯誤對象,將它們賦給處理方法的相應入參。
總結起來:大致流程是 綁定(bindingResult)--》數據轉換(conversionService)--》校驗(validators)
數據轉換
SpringMvc上下文中內建了很多轉換器,可以完成大多數Java類型轉換工作。但是如果就要轉換成我們自定義的類型時,那么我們就要自定義類型轉換器,並將其加入到conversionService中去,conversionService中包含了很多SpringMvc內建的轉換器。
ConversionService是SpringMvc類型轉換體系的核心接口,可以利用ConversionServiceFactoryBean在Spring的IOC容器中定義一個ConversionService,Spring將自動識別出IOC容器中的ConversionService,並在bean屬性配置及SpringMvc處理方法入參綁定等場合使用它進行數據轉換。
首先,定義一個轉換器:
public class Department {
private Integer id;
private String departmentName;
...........
}
public class Employee {
private Integer id;
private String name;
private String email;
private Integer gender;
private Department department;
private Date birth;
private double salary;
......
}
package com.seven.converts;
import com.seven.domain.Department;
import com.seven.domain.Employee;
import org.springframework.core.convert.converter.Converter;
import org.springframework.stereotype.Component;
/**
* Created by hu on 2016/4/3.
*/
@Component
//該類的主要目的是將字符串轉換為一個Employee對象
public class EmployeeConverts implements Converter<String,Employee> {
@Override
/*
* source就是前台web頁面傳遞過來的字符串
* 如:gg-gg@qq.com-0-105 姓名-郵件-性別-部門ID
* */
public Employee convert(String source) {
if(source!=null){
String[] vals=source.split("-");
//獲得雇員的姓名
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,department,gender,email,name);
return employee;
}
//如果字符串為空,就不生成雇員對象
return null;
}
}
在SpringMvc的配置文件中配置轉換器:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--配置自動掃描的包-->
<context:component-scan base-package="com.seven"></context:component-scan>
<!--配置視圖解析器,將視圖邏輯名解析為/WEB-INF/pages/<viewName>.jsp-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!--將自定義的轉換器加入到框架中-->
<bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean">
<property name="converters">
<set>
<bean class="com.seven.converts.EmployeeConverts"/>
</set>
</property>
</bean>
<mvc:annotation-driven/>
<mvc:default-servlet-handler/>
</beans>
Spring支持三種類型的轉換器接口,實現任意一個轉換器接口都可以作為自定義轉換器注冊ConversionServiceFactoryBean中:
-Converter<S,T>:將S類型的對象轉為T類型對象
-ConverterFactory:將相同系列多個"同質"Converter封裝在儀器,如果希望將一種類型的對象轉換為一種類型及其子類的對象(例如將String轉化為Number及Number的子類)
-GenericConverter:會根據源類對象與目標類對象所在的宿主類中的上下文信息進行類型轉換。<mvc:annotation-driven conversion-service="conversionService"/>會將自定義的ConversionService注冊到SpringMvc的上下文中去。
關於mvc:annotation-driven
<mvc:annotation-driven/>會自動注冊ReuqestMappingHandlerMapping、ReuqestMappingHandlerHandler、ExceptionHanderExceptionResolver三個bean。還提供以下支持:
-支持使用ConversionService實例對表單參數進行類型轉換
-支持使用@NumberFormat annotation @DateTimeFormat 注解完成數據類型的格式化
-支持使用@Valid注解對JavaBean實例進行JSR303驗證
-支持使用@RequestBody 和@ResponseBody注解
