springMVC中bean中定义了Date,double,Integer等类型,表单提交的数据(字符串)无法做转换为实体对象的属性,因此需要@InitBinder做数据类型转换
那么spring mvc在绑定表单之前,都会先注册CustomBooleanEditor,CustomNumberEditor等这些编辑器,当然你如果不嫌麻烦,你也可以单独的写在你的每一个controller中。
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));
}
}
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;
}
}
springMVC中bean中定义了Date,double,Integer等类型,表单提交的数据(字符串)无法做转换为实体对象的属性,因此需要@InitBinder做数据类型转换
那么spring mvc在绑定表单之前,都会先注册CustomBooleanEditor,CustomNumberEditor等这些编辑器,当然你如果不嫌麻烦,你也可以单独的写在你的每一个controller中。
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));
}
}
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;
}
}