spring mvc 表单的实体包含时间Date,double等类型的处理
来源:互联网 发布:程序员桌子上放菜刀 编辑:程序博客网 时间:2024/06/03 06:28
在SpringMVC中,bean中定义了Date,double等类型,如果没有做任何处理的话,日期以及double都无法绑定。
解决的办法就是使用spring mvc提供的@InitBinder标签
在我的项目中是在BaseController中增加方法initBinder,并使用注解@InitBinder标注,那么spring mvc在绑定表单之前,都会先注册这些编辑器,当然你如果不嫌麻烦,你也可以单独的写在你的每一个controller中。剩下的控制器都继承该类。spring自己提供了大量的实现类,诸如CustomDateEditor ,CustomBooleanEditor,CustomNumberEditor等许多,基本上够用。
在controller里加入
@InitBinder protected void initBinder(WebDataBinder binder) { SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true)); }方法就可以
或者使用注解方式
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone="GMT+8") //取日期时使用 @DateTimeFormat(pattern = "yyyy-MM-dd")//存日期时使用 private Date startTime;
当然,我们也可以不使用他自己自带这些编辑器类,那下面我们自己去构造几个
- import org.springframework.beans.propertyeditors.PropertiesEditor;
- public class DoubleEditor extends PropertiesEditor {
- @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();
- }
- }
- import org.springframework.beans.propertyeditors.PropertiesEditor;
- public class IntegerEditor extends PropertiesEditor {
- @Override
- public void setAsText(String text) throws IllegalArgumentException {
- if (text == null || text.equals("")) {
- text = "0";
- }
- setValue(Integer.parseInt(text));
- }
- @Override
- public String getAsText() {
- return getValue().toString();
- }
- }
- import org.springframework.beans.propertyeditors.PropertiesEditor;
- public class FloatEditor extends PropertiesEditor {
- @Override
- public void setAsText(String text) throws IllegalArgumentException {
- if (text == null || text.equals("")) {
- text = "0";
- }
- setValue(Float.parseFloat(text));
- }
- @Override
- public String getAsText() {
- return getValue().toString();
- }
- }
- import org.springframework.beans.propertyeditors.PropertiesEditor;
- public class LongEditor extends PropertiesEditor {
- @Override
- public void setAsText(String text) throws IllegalArgumentException {
- if (text == null || text.equals("")) {
- text = "0";
- }
- setValue(Long.parseLong(text));
- }
- @Override
- public String getAsText() {
- return getValue().toString();
- }
- }
在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());
- }
- public class org.springframework.beans.propertyeditors.PropertiesEditor extends java.beans.PropertyEditorSupport {
看到没?如果你的编辑器类直接继承PropertyEditorSupport也可以。
0 0
- spring mvc 表单的实体包含时间Date,double等类型的处理
- spring mvc处理提交的时间类型的参数
- Date、Double等类型转化,oracle日期处理
- Spring MVC 之 处理Date类型
- Spring MVC 之 处理Date类型
- spring mvc:处理表单上的多选框
- springMVC 表单数据不能绑定 Date double 等数据类型的问题
- springmvc使用实体类接收表单提交数据中含有String类型对应Date类型的不匹配报错400时如何处理
- springMVC form表单提交---包含时间类型的数据
- springMVC form表单提交---包含时间类型的数据
- springMVC form表单提交---包含时间类型的数据
- springMVC form表单提交---包含时间类型的数据
- springMVC form表单提交---包含时间类型的数据
- spring mvc表单数据绑定,对于基本类型和日期的处理WebDataBinder
- spring mvc表单数据绑定,对于基本类型和日期的处理WebDataBinder
- Spring MVC中对Date类型操作的一些问题解决
- spring mvc使用GET请求获取Date类型的方法
- Spring MVC 映射Date类型参数的解决方案
- Thinking in C++ chapter 2
- java.io.CharConversionException: isHexDigit.
- Oracle 10g 的 jdbc 驱动知识
- (视频) 《快速创建网站》 3.2 WordPress多站点及Azure在线编辑器 - 扔掉你的ftp工具吧,修改代码全部云端搞定
- 逻辑运算符
- spring mvc 表单的实体包含时间Date,double等类型的处理
- delphiXE 使用json对象时只释放容器对象即可,子对象不用释放,释放子对象会出现异常。
- 在IE下AJAX失效,返回的是上一次的结果
- 泛型子类获取父类的模板类型方法
- 大数计算
- onSaveInstanceState和onRestoreInstanceState触发的时机
- 使用LogonUser来代替WNetAddConnection2A来访问共享文件夹
- 电脑连接android智能电视的一种方法
- .NET中常见的内存泄露问题——GC、委托事件和弱引用