SpringMVC的@InitBinder注解使用

简介:

@InitBinder用于在@Controller中标注于方法,表示为当前控制器注册一个属性编辑器或者其他,只对当前的Controller有效。在使用SpringMVC的时候,经常会遇到表单中的日期字符串和JavaBean的Date类型的转换,而SpringMVC默认不支持这个格式的转换,所以需要手动配置,自定义数据的绑定才能解决这个问题。在需要日期转换的Controller中使用SpringMVC的注解@initbinder和Spring自带的WebDateBinder类来操作。

WebDataBinder是用来绑定请求参数到指定的属性编辑器.由于前台传到controller里的值是String类型的,当往Model里Set这个值的时候,如果set的这个属性是个对象,Spring就会去找到对应的editor进行转换,然后再SET进去。

代码如下:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package  com.simple.database.controller;
 
import  java.text.DateFormat;
import  java.text.ParseException;
import  java.text.SimpleDateFormat;
import  java.util.Date;
import  java.util.HashMap;
import  java.util.Map;
 
import  org.springframework.beans.propertyeditors.CustomDateEditor;
import  org.springframework.stereotype.Controller;
import  org.springframework.web.bind.WebDataBinder;
import  org.springframework.web.bind.annotation.InitBinder;
import  org.springframework.web.bind.annotation.RequestMapping;
import  org.springframework.web.bind.annotation.RequestMethod;
import  org.springframework.web.bind.annotation.ResponseBody;
 
@RequestMapping ( "test" )
@Controller
public  class  TestController {
 
     @InitBinder
     public      void  InitBinder(WebDataBinder binder){
         DateFormat df =  new  SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" );
         CustomDateEditor dateEditor =  new  CustomDateEditor(df,  true );
         binder.registerCustomEditor(Date. class ,dateEditor);
     }
     
     @RequestMapping (value= "/param" ,method=RequestMethod.GET)
     @ResponseBody
     public  Map<String,Object> getFormatData(Date date)  throws  ParseException{
         Map<String,Object> map =  new  HashMap<String, Object>();
         map.put( "name" "zhangsan" );
         map.put( "age" 22 );
         map.put( "date" ,date);
         return  map;
     }
}

需要在SpringMVC的配置文件加上


<!-- 解析器注册 -->  

1
2
3
4
5
6
7
8
9
<bean  class = "org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter" >  
     <property name= "messageConverters" >  
         <list>  
             <ref bean= "stringHttpMessageConverter" />  
         </list>  
     </property>  
</bean>  
<!-- String类型解析器,允许直接返回String类型的消息 -->  
<bean id= "stringHttpMessageConverter"  class = "org.springframework.http.converter.StringHttpMessageConverter" />

 

换种写法

1
2
3
4
5
6
7
8
9
10
<!-- 注解驱动 -->
<mvc:annotation-driven>
     <!-- 如果自定义message-converters,默认的message-converters将失效 -->
     <mvc:message-converters>
         <!-- 定义文本转化器 -->
         <bean  class = "org.springframework.http.converter.StringHttpMessageConverter" >
             <constructor-arg index= "0"  value= "UTF-8"  />
         </bean>
     </mvc:message-converters>
</mvc:annotation-driven>

拓展:

spring mvc在绑定表单之前,都会先注册这些编辑器,Spring自己提供了大量的实现类,诸如CustomDateEditor ,CustomBooleanEditor,CustomNumberEditor等许多,基本上够用。

使用时候调用WebDataBinder的registerCustomEditor方法

registerCustomEditor源码:


public void registerCustomEditor(Class<?> requiredType, PropertyEditor propertyEditor) {

    getPropertyEditorRegistry().registerCustomEditor(requiredType, propertyEditor);

}

第一个参数requiredType是需要转化的类型。

第二个参数PropertyEditor是属性编辑器,它是个接口,以上提到的如CustomDateEditor等都是继承了实现了这个接口的PropertyEditorSupport类。

我们也可以不使用他们自带的这些编辑器类。

我们可以自己构造:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import  org.springframework.beans.propertyeditors.PropertiesEditor;
 
public  class  DoubleEditor  extends  PropertyEditorSupport {
     @Override
     public  void  setAsText(String text)  throws  IllegalArgumentException {
         if  (text ==  null  || text.equals( "" )) {
             text =  "0" ;
         }
         setValue(Double.parseDouble(text));
     }
 
     @Override
     public  String getAsText() {
         return  getValue().toString();
     }
}



本文转自 兴趣e族 51CTO博客,原文链接:http://blog.51cto.com/simplelife/1919597

相关文章
|
XML 数据格式
SpringMVC - 数据绑定(Xml、@InitBinder、Set、嵌套对象、多个对象)(二)
SpringMVC - 数据绑定(Xml、@InitBinder、Set、嵌套对象、多个对象)(二)
189 0
SpringMVC中利用@InitBinder来对页面数据进行解析绑定
  同步发布:http://www.yuanrengu.com/index.php/springmvc-user-initbinder.html   在使用SpingMVC框架的项目中,经常会遇到页面某些数据类型是Date、Integer、Double等的数据要绑定到控制器的实体,或者控制器需要接受这些数据,如果这类数据类型不做处理的话将无法绑定。
1128 0
SpringMVC 中 @InitBinder
有@InitBander标识的方法,可以对WebDataBinder对象进行初始化。WebDataBinder是DataBinder的子类,用于完成由表单字段到JavaBean属性 绑定。 @InitBinder方法不能有返回值,它必须声明为void。 @InitBinder方法的参数通常是WebDataBinder。 我在我的Handler里面加上这样一个方法,用到se
1239 0
|
8天前
|
设计模式 前端开发 JavaScript
Spring MVC(一)【什么是Spring MVC】
Spring MVC(一)【什么是Spring MVC】
|
8天前
|
Java Apache vr&ar
springmvc报错 nested exception is org.mybatis.spring.MyBatisSystemException:
springmvc报错 nested exception is org.mybatis.spring.MyBatisSystemException:
17 0
|
7月前
|
前端开发 Java Go
Spring MVC 和 Spring Boot 的区别
Spring MVC 和 Spring Boot 的区别
116 0
|
10月前
|
Java Spring
springmvc中spring提供的中文乱码解决问题
可以解决浏览器的乱码问题
47 0
|
10月前
|
JSON 前端开发 Java
"《图书管理系统》利用SpringMvc$Spring$MyBatis (实操九)(一) "
"《图书管理系统》利用SpringMvc$Spring$MyBatis (实操九)(一) "
59 0