<mvc:annotation-driven /> 或 <bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean" />
其实 <mvc:annotation-driven /> 标签会默认创建并注册一个 RequestMappingHandlerMapping(在Spring3.2之前是DefaultAnnotationHandlerMapping) 和 RequestMappingHandlerAdapter (Spring3.2之前是AnnotationMethodHandlerAdapter),当然,如果上下文有对应的显示实现类,将该注解注册的覆盖掉。该注解还会创建一个ConversionService,即 FormattingConversionServiceFactoryBean。
如果想把一个字符串转换成其它实体类型,spring没有提供这样默认的功能,我们需要自定义类型转换器。
这里有个实体类Employee。
1 package com.proc; 2 3 public class Employee { 4 5 private String name; 6 private Integer age; 7 private String gender; 8 public String getName() { 9 return name; 10 } 11 public void setName(String name) { 12 this.name = name; 13 } 14 public Integer getAge() { 15 return age; 16 } 17 public void setAge(Integer age) { 18 this.age = age; 19 } 20 public String getGender() { 21 return gender; 22 } 23 public void setGender(String gender) { 24 this.gender = gender; 25 } 26 @Override 27 public String toString() { 28 return "Employee [name=" + name + ", age=" + age + ", gender=" + gender 29 + "]"; 30 } 31 32 }
规定将字符串转换成实体类的规则为: name-age-gender
那么接下来就写一个类型转换器,需要实现一个接口org.springframework.core.convert.converter.Converter
1 package com.proc; 2 3 import org.springframework.core.convert.converter.Converter; 4 import org.springframework.stereotype.Component; 5 6 @Component 7 public class EmployeeConverter implements Converter<String, Employee> { 8 9 @Override 10 public Employee convert(String str) { 11 Employee emp=null; 12 //字符串格式 name-age-gender 13 if(str!=null && str.split("-").length==3){ 14 emp=new Employee(); 15 String[] arr=str.split("-"); 16 emp.setName(arr[0]); 17 emp.setAge(Integer.parseInt(arr[1])); 18 emp.setGender(arr[2]); 19 } 20 return emp; 21 } 22 }
这里我们为转换器加上@Component注解,是为了让Spring自动扫描该转换器到容器中。这样就不用通过配置文件配置<bean>了
接下来编写控制器
1 package com.proc; 2 3 import org.springframework.stereotype.Controller; 4 import org.springframework.web.bind.annotation.RequestMapping; 5 import org.springframework.web.bind.annotation.RequestParam; 6 7 @Controller 8 public class EmployeeController { 9 10 @RequestMapping("/add") 11 public String add(@RequestParam("employee") Employee employee){ 12 System.out.println(employee); 13 return "add"; 14 } 15 }
这里可以看到,参数的名字为employee,所有要为请求定义一个employee参数,该参数传入需要转换的字符串
除此之外,我们还需要在spring配置文件中配置,如下类容。让转换器生效
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xmlns:mvc="http://www.springframework.org/schema/mvc" 6 xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd 7 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd 8 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd"> 9 10 <context:component-scan base-package="com.proc"></context:component-scan> 11 12 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 13 <property name="prefix" value="/WEB-INF/"/> 14 <property name="suffix" value=".jsp"></property> 15 </bean> 16 17 <mvc:annotation-driven conversion-service="conversionService"/> 18 19 <bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean"> 20 <property name="converters"> 21 <set> 22 <ref bean="employeeConverter"/> 23 </set> 24 </property> 25 </bean> 26 </beans>
测试代码:http://localhost:8080/springmvc-1/add?employee=caoyc-18-male
结果输出:Employee [name=caoyc, age=18, gender=male]
上面配置文件中我们使用配置了一个ConversionServiceFactoryBean。虽然可以这样,但不建议。我们通常用org.springframework.format.support.FormattingConversionServiceFactoryBean
为什么要这样用呢?这样用的好处是什么呢?
使用FormattingConversionServiceFactoryBean可以让SpringMVC支持@NumberFormat和@DateTimeFormat等Spring内部自定义的转换器。