您当前的位置: 首页 > 

Charge8

暂无认证

  • 3浏览

    0关注

    447博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

@InitBinder注解绑定表单数据

Charge8 发布时间:2018-09-27 22:28:20 ,浏览量:3

 

在SpringMVC框架的项目中,bean中定义了Date,double等类型,如果没有做任何处理的话,日期以及double都无法绑定。

     比如经常会遇到页面表单中某些数据类型是Date、Integer、Double等的数据要绑定到控制器的实体,或者控制器需要接受这些数据,如果这类数据类型不做处理的话将无法绑定。而springMVC默认不支持这个格式的转换,所以必须要手动配置, 自定义数据类型的绑定才能实现这个功能,比较简单的可以直接应用springMVC的注解@initbinder和spring自带的WebDataBinder类和操作。

 

一、常用的日期转换有三种方式:

   参考文章:  @JsonFormat与@DateTimeFormat注解的使用

1. POJO类的属性添加@DateTimeFormat(pattern = “yyyy-MM-dd”)注解,可以实现string转换成Date类型。
	@DateTimeFormat(pattern="yyyy-MM-dd")
	private Date birthday;

 

2.  web层的开发添加 @InitBinder 注解,实现方法public void initBinder(WebDataBinder binder)。

使用@InitBinder来注解解决这些问题,这样SpingMVC在绑定表单之前,都会先注册这些编辑器。一般我们会将这些方法写在BaseController类中,需要进行这类转换的控制器只需继承BaseController即可。

Spring自己提供了大量的编辑器实现类,如CustomDateEditor,CustomBooleanEditor,CustomNumberEditor等,基本够用了。

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

springMVC配置文件需要配置:

public class BaseController {
	@InitBinder
	public void initBinder(WebDataBinder binder) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		//设置指定日期/时间解析是否宽松解析字符串。
		//默认true:宽松解析字符串,输入“33/12/2011”,用SimpleDateFormat parse()方法,转化为Date(2012,01,02)。
		//false: 严格解析字符串,输入必须是正常格式,不进位转换。
		sdf.setLenient(false);		
		binder.registerCustomEditor(Date.class, new CustomDateEditor(sdf, true));//允许为空
	}
}

 需要进行这类转换的控制器只需继承BaseController即可

@Controller
public class UserController extends BaseController{
    ...
}
  3. 在项目中加入一个全局自定义的数据转换器(springmvc自定义数据转换器)

首先自定义一个全局类型转换器类DateConverter:

package cn.jq.sshweb.web;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.core.convert.converter.Converter;

public class DateConverter implements Converter{
	@Override
	public Date convert(String source) {
		if(source != null) {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			sdf.setLenient(false);
			try {
				return sdf.parse(source);	//将字符串解析成日期类型
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}
		return null;
	}

}

注册到配置文件SpringMVC.xml中,即可

    
    

    

	
		
                        
			
				
				
			
		
	

小结:

第一种方式是最简单的,但是也要根据实际情况去实现,如果你只有开发web层代码的权限,这第二、三种方式比较适合。

第二种方式比较冗余,因为你要为每一个controller控制器都注册一个日期转换器,所以比较麻烦,也是最不常用的。

第三种方式,自定义一个系统全局日期转换器,配置简单,实现也是比较简单的。

 

二、我们也可以不用SpringMVC自带的编辑器,可以自定义编辑器editer

首先自定义自己的类型编译器直接继承PropertyEditorSupport:举例处理Date类型, 其他类型处理类似

import java.beans.PropertyEditorSupport;

public class MyDoubleEditor extends PropertyEditorSupport {

	@Override
	public void setAsText(String text) throws IllegalArgumentException {
		if (text == null || text.equals("")) {
			text = "0";
		}
		Double db = Double.parseDouble(text)+100;//假设加100
		setValue(db);
	}

	@Override
	public String getAsText() {
		return getValue().toString();
	}
}

然后,WebDataBinder是用来绑定请求参数到指定的属性编辑器.

import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;

import cn.jq.sshweb.web.MyDoubleEditor;

public class BaseController {

	@InitBinder
	public void initBinder(WebDataBinder binder) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		//设置指定日期/时间解析是否宽松解析字符串。
		//默认true:宽松解析字符串,输入“33/12/2011”,用SimpleDateFormat parse()方法,转化为Date(2012,01,02)。
		//false: 严格解析字符串,输入必须是正常格式,不进位转换。
		sdf.setLenient(false);		
		binder.registerCustomEditor(Date.class, new CustomDateEditor(sdf, true));//允许为空
		//自定义编译器
		binder.registerCustomEditor(Double.class, new MyDoubleEditor());
	}
}

最后 控制器继承 BaseController 即可。

 

 

关注
打赏
1664721914
查看更多评论
立即登录/注册

微信扫码登录

0.0380s