springboot @InitBinder 标签对表单数据绑定

文章参考

  1. spring mvc使用@InitBinder 标签对表单数据绑定

@InitBinder有什么作用

springMVC中bean中定义了Date,double,Integer等类型,表单提交的数据(字符串)无法做转换为实体对象的属性,因此需要@InitBinder做数据类型转换

springMVC 表单注入对象的实现原理

那么spring mvc在绑定表单之前,都会先注册CustomBooleanEditor,CustomNumberEditor等这些编辑器,当然你如果不嫌麻烦,你也可以单独的写在你的每一个controller中。

在Controller类中添加@InitBinder注解方法解决

package com.hb.goods.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TimeZone;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.hb.goods.bean.Goods;
import com.hb.goods.service.GoodsService;
import com.hb.util.BaseController;

@RestController
@RequestMapping(value="/pssGoods")
public class GoodsController extends BaseController{
	
	@Autowired
	private GoodsService goodsService;
	
	/**
	 * 接受前端传递过来的对象;要求表单的name属性与实体对象的属性一致
	 * @param goodsObj
	 * @return
	 */
	@RequestMapping(value="/addGoods", method= {RequestMethod.GET})
	public Map addGoods(Goods goodsObj) {
		System.out.println(goodsObj.getGoodsName());
		
		goodsService.saveGoods(goodsObj);
		Map hashMap = new HashMap();
		hashMap.put("resultCode", 1);
		return hashMap;
//		return "{resultCode: 1}";
	}
	
	@RequestMapping( value = "/findByGoodsName/{goodsName}")
	@ResponseBody
	public List<Goods> findByGoodsName(@PathVariable("goodsName") String goodsName) {
		List<Goods> list = goodsService.findGoodsByGoodsName(goodsName);
		return list;
	}
	
	/**
	 * @InitBinder表示的方法,可以对WebDataBinder对象进行初始化。
	 * WebDataBinder是DataBinder的子类,用于完成由表单到JavaBean属性的绑定。
	 * @param request
	 * @param binder
	 * 该方法已经交由 BaseController类中写入
	 */
	@InitBinder
    protected void init(HttpServletRequest request, ServletRequestDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));/*TimeZone时区,解决差8小时的问题*/
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false));
    }
}

在Controller类集成BaseController类(有@InitBinder方法实现类型转换)

package com.hb.util;

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

import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import com.sun.beans.editors.*;

public class BaseController {
	@InitBinder  
    protected void initBinder(WebDataBinder binder) {  
        binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"), true));  
//        binder.registerCustomEditor(int.class, new CustomNumberEditor(int.class, true));  
        binder.registerCustomEditor(int.class, new IntegerEditor());  
//        binder.registerCustomEditor(long.class, new CustomNumberEditor(long.class, true));
        binder.registerCustomEditor(long.class, new LongEditor());  
        binder.registerCustomEditor(double.class, new DoubleEditor());  
        binder.registerCustomEditor(float.class, new FloatEditor());  
    } 
}

package com.hb.goods.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TimeZone;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.hb.goods.bean.Goods;
import com.hb.goods.service.GoodsService;
import com.hb.util.BaseController;

@RestController
@RequestMapping(value="/pssGoods")
public class GoodsController extends BaseController{
	
	@Autowired
	private GoodsService goodsService;
	
	/**
	 * 接受前端传递过来的对象;要求表单的name属性与实体对象的属性一致
	 * @param goodsObj
	 * @return
	 */
	@RequestMapping(value="/addGoods", method= {RequestMethod.GET})
	public Map addGoods(Goods goodsObj) {
		System.out.println(goodsObj.getGoodsName());
		
		goodsService.saveGoods(goodsObj);
		Map hashMap = new HashMap();
		hashMap.put("resultCode", 1);
		return hashMap;
//		return "{resultCode: 1}";
	}
	
	@RequestMapping( value = "/findByGoodsName/{goodsName}")
	@ResponseBody
	public List<Goods> findByGoodsName(@PathVariable("goodsName") String goodsName) {
		List<Goods> list = goodsService.findGoodsByGoodsName(goodsName);
		return list;
	}
	
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值