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

解决的办法就是使用spring mvc提供的@InitBinder标签

在我的项目中是在BaseController中增加方法initBinder,并使用注解@InitBinder标注,那么spring mvc在绑定表单之前,都会先注册这些编辑器,当然你如果不嫌麻烦,你也可以单独的写在你的每一个controller中。剩下的控制器都继承该类。spring自己提供了大量的实现类,诸如CustomDateEditor
,CustomBooleanEditor,CustomNumberEditor等许多,基本上够用。

当然,我们也可以不使用他自己自带这些编辑器类,那下面我们自己去构造几个

    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());
}
  1. public class org.springframework.beans.propertyeditors.PropertiesEditor extends java.beans.PropertyEditorSupport {

如果你的编辑器类直接继承PropertyEditorSupport也可以。

spring mvc使用@InitBinder 标签对表单数据绑定的更多相关文章

  1. fieldset 标签 -- 对表单进行分组

    转自:https://xhmaomy-163-com.iteye.com/blog/1066977 fieldset——一个不常用的HTML标签 fieldset 标签 -- 对表单进行分组 在for ...

  2. Spring MVC框架及标签库

    1.Spring MVC技术 1. 当DispatcherServlet接到请求时,他先回查找适当的处理程序来处理请求.DispatcherServlet通过一个或者多个处理程序映射,将每个请求映射到 ...

  3. Spring MVC -- JSP标准标签库(JSTL)

    JSP标准标签库(JavaServer Pages Standard Tag Library,JSTL)是一个JSP标签集合,它封装了JSP应用的通用核心功能.JSTL支持通用的.结构化的任务,比如迭 ...

  4. spring mvc:常用标签库(文本框,密码框,文本域,复选框,单选按钮,下拉框隐藏于,上传文件等)

    在jsp页面需要引入:<%@taglib uri="http://www.springframework.org/tags/form" prefix="form&q ...

  5. spring mvc 防止重复提交表单的两种方法,推荐第二种

    第一种方法:判断session中保存的token 比较麻烦,每次在提交表单时都必须传入上次的token.而且当一个页面使用ajax时,多个表单提交就会有问题. 注解Token代码: package c ...

  6. spring mvc 使用jsr-303进行表单验证的方法介绍

    源代码来源:http://howtodoinjava.com/spring/spring-mvc/spring-bean-validation-example-with-jsr-303-annotat ...

  7. spring mvc常用注解标签

    @Controller 在SpringMVC 中,控制器Controller 负责处理由DispatcherServlet 分发的请求,它把用户请求的数据经过业务处理层处理之后封装成一个Model , ...

  8. Struts2 和 spring mvc的 迭代标签常用属性对比

    <s:iterator value="#users" var="u" status="st"> <c:forEach  i ...

  9. 基于Spring MVC实现基于form表单上传Excel文件,批量导入数据

    在pom.xml中引入: <!--处理2003 excel--> <dependency> <groupId>org.apache.poi</groupId& ...

随机推荐

  1. Lae程序员小漫画(二),仅供一乐

    Lae软件开发,快乐程序员!

  2. C++ 中queue(队列)的用法

    #include <iostream>#include <queue>#include <assert.h>/*调用的时候要有头文件: #include<st ...

  3. IplImage结构体

    一.IplImage的一些重要成员: 1.origin:图像原点的定义.=0,则图片的左上角是原点:=1,则左下角是原点.                                  IplIm ...

  4. div隐藏与显示

    <input type="button" value="隐藏详情" class="jishu_n_k1_input2" id=&quo ...

  5. java IO基础操作

    java IO基础,通熟易懂,好久没复习java 基础了.这里是传送门... http://www.cnblogs.com/nerxious/archive/2012/12/15/2818848.ht ...

  6. 第四篇T语言实例开发,自动加血

    游戏自动加血 基础知识复习 通过前面的学习了解以下内容: TC软件的基本使用 TC的基础语法 变量与常量 功能的使用 流程语句的使用 线程的基本使用 TC控件的基本使用 热键和按钮的事件功能 控件的数 ...

  7. spring mvc 4识别浏览器(移动端) spring-mobile-device

    官方文档: http://projects.spring.io/spring-mobile/ 通过mvc.xml配置示例: http://blog.csdn.net/wuyt2008/article/ ...

  8. 6个错误将杀死你的App

    没有开发者或者设计师会故意破坏应用的设计.所有的应用程序创建者都对自己的应用寄予美好的愿望,但是很多错误是在他们无意识的状态下破坏app的设计.以下是应用开发者和设计者经常犯的几个错误,不过这些错误是 ...

  9. PChar,PAnsiChar,String,AnsiString,Char数组,AnsiChar数组转换

    PChar,PAnsiChar,String,AnsiString,Char数组,AnsiChar数组之间的转换关系见下图 通过转换链,可以实现任意两个类型之间的互转.如PChar转PAnsiChar ...

  10. 13、java中的多态

    1,多态的体现 父类的引用指向了自己的子类对象. 父类的引用也可以接收自己的子类对象.2,多态的前提 必须是类与类之间有关系.要么继承,要么实现. 通常还有一个前提:存在覆盖. 3,多态的好处 多态的 ...