在通过处理器方法参数接收request请求参数绑定数据的时候,对于一些简单的数据类型Spring会帮我们自动进行类型转换,而对于一些复杂的类型由于Spring没法识别,所以也就不能帮助我们进行自动转换了,这个时候如果我们需要Spring来帮我们自动转换的话就需要我们给Spring注册一个对特定类型的识别转换器。Spring 允许我们提供两种类型的识别转换器,一种是注册在Controller中的,一种是注册在 SpringMVC 的配置文件中。聪明的读者看到这里应该可以想到它们的区别了,定义在Controller中的是局部的,只在当前Controller中有效,而放在SpringMVC配置文件中的是全局的,所有 Controller 都可以拿来使用。
我们可以使用@InitBinder注解标注在Controller方法上,然后在方法体里面注册数据绑定的转换器,这主要是通过 WebDataBinder 进行的。我们可以给需要注册数据绑定的转换器的方法一个 WebDataBinder 参数,然后给该方法加上 @InitBinder 注解,这样当该Controller中在处理请求方法时如果发现有不能解析的对象的时候,就会看该类中是否有使用@InitBinder标记的方法,如果有就会执行该方法,然后看里面定义的类型转换器是否与当前需要的类型匹配。看代码:
@Controller @RequestMapping("/testInit") public class InitialController { @InitBinder public void dataBinder(WebDataBinder binder) { System.out.println("dataBinder--->"); DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd"); PropertyEditor propertyEditor = new CustomDateEditor(dateFormat, true); // 第二个参数表示是否允许为空 binder.registerCustomEditor(Date.class, propertyEditor); } @RequestMapping("/testDate/{date}") public void testDate(@PathVariable("date") Date date, Writer writer) { DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); try { writer.write("testDate:" + date); writer.write("\ngetTime:" + date.getTime()); writer.write("\nfromatTime:" + dateFormat.format(date)); } catch (IOException e) { e.printStackTrace(); } } }
测试结果如下:
并且在后台dataBinder有打印,说明先通过了类型转换器的转换方法。
如果需要定义全局的类型转换器就需要实现自己的WebBindingInitializer对象,然后把该对象注入到RequestMappingHandlerAdapter中,这样Spring在遇到自己不能解析的对象的时候就会到全局的WebBindingInitializer的initBinder方法中去找,每次遇到不认识的对象时,initBinder方法都会被执行一遍。先看WebBindingInitializer的实现类:
public class MyWebBindingInitializer implements WebBindingInitializer { public void initBinder(WebDataBinder binder, WebRequest request) { PropertyEditor userEditor = new PropertyEditorSupport() { @Override public String getAsText() { System.out.println("getAsText--->"); User user = (User) getValue(); return user.getName(); } @Override public void setAsText(String userStr) throws IllegalArgumentException { System.out.println("setAsText--->" + userStr); User user = new User(); user.setName(userStr); setValue(user); } }; binder.registerCustomEditor(User.class, userEditor); DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd"); PropertyEditor propertyEditor = new CustomDateEditor(dateFormat, true); binder.registerCustomEditor(Date.class, propertyEditor); } }此类有两个特点:
1、此类的转换器方法注册了两个PropertyEditor,分别是用来转换User.class、Date.class。
2、转换User.class的PropertyEditor是自定义的,转换Date.class的是自带的。
但是如果要是想用这个转换器还需要在配置文件中配置如下代码,需要注意的是,下面配置适合spring3.0以上的版本:
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"> <property name="cacheSeconds" value="0" /> <property name="webBindingInitializer"> <bean class="com.wangpeng.testmodule.initialezers.MyWebBindingInitializer" /> </property> </bean>下面看一下测试类:
@Controller @RequestMapping("testBinder") public class BinderController { @RequestMapping("testUserDate") public void testUserDate(@RequestParam(value = "user") User user, @RequestParam(value = "date") Date date, HttpServletRequest request, HttpServletResponse response) { try { request.setCharacterEncoding("utf-8"); response.setCharacterEncoding("UTF-8"); response.setHeader("content-type", "text/html;charset=UTF-8"); Writer writer = response.getWriter(); DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); writer.write("user:" + user.getName()); writer.write("<br>getTime:" + date.getTime()); writer.write("<br>fromatTime:" + dateFormat.format(date)); } catch (IOException e) { e.printStackTrace(); } } }测试结果如下: