spring mvc使用@InitBinder 标签对表单数据绑定
在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());
}
- public class org.springframework.beans.propertyeditors.PropertiesEditor extends java.beans.PropertyEditorSupport {
如果你的编辑器类直接继承PropertyEditorSupport也可以。
spring mvc使用@InitBinder 标签对表单数据绑定的更多相关文章
- fieldset 标签 -- 对表单进行分组
转自:https://xhmaomy-163-com.iteye.com/blog/1066977 fieldset——一个不常用的HTML标签 fieldset 标签 -- 对表单进行分组 在for ...
- Spring MVC框架及标签库
1.Spring MVC技术 1. 当DispatcherServlet接到请求时,他先回查找适当的处理程序来处理请求.DispatcherServlet通过一个或者多个处理程序映射,将每个请求映射到 ...
- Spring MVC -- JSP标准标签库(JSTL)
JSP标准标签库(JavaServer Pages Standard Tag Library,JSTL)是一个JSP标签集合,它封装了JSP应用的通用核心功能.JSTL支持通用的.结构化的任务,比如迭 ...
- spring mvc:常用标签库(文本框,密码框,文本域,复选框,单选按钮,下拉框隐藏于,上传文件等)
在jsp页面需要引入:<%@taglib uri="http://www.springframework.org/tags/form" prefix="form&q ...
- spring mvc 防止重复提交表单的两种方法,推荐第二种
第一种方法:判断session中保存的token 比较麻烦,每次在提交表单时都必须传入上次的token.而且当一个页面使用ajax时,多个表单提交就会有问题. 注解Token代码: package c ...
- spring mvc 使用jsr-303进行表单验证的方法介绍
源代码来源:http://howtodoinjava.com/spring/spring-mvc/spring-bean-validation-example-with-jsr-303-annotat ...
- spring mvc常用注解标签
@Controller 在SpringMVC 中,控制器Controller 负责处理由DispatcherServlet 分发的请求,它把用户请求的数据经过业务处理层处理之后封装成一个Model , ...
- Struts2 和 spring mvc的 迭代标签常用属性对比
<s:iterator value="#users" var="u" status="st"> <c:forEach i ...
- 基于Spring MVC实现基于form表单上传Excel文件,批量导入数据
在pom.xml中引入: <!--处理2003 excel--> <dependency> <groupId>org.apache.poi</groupId& ...
随机推荐
- xmind的第十三天笔记
- 在Android Studio中使用lambda表达式
build.gradle中添加以下配置 Android{ ..... compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targ ...
- 使用gnucash查看任意时间段内的所有者权益变动表
gnucash默认会以当年的会计账期显示所有者权益变动表 如果要看指定时间的所有者权益变动表,需要这样做 打开所有者权益变动表 点击上面的[选项] 如图 在常规中自行选择日期 确定后就能够看到指定时间 ...
- IIC
IIC多主从,双向传输,只有两根线:一根数据,一根时钟,时钟必须由主机发出控制.初始化时主机把SCL和SDA的电平都拉高,然后在SCL保持高电平时SDA拉低形成一个开始信号,紧接着开始信号就开始发送要 ...
- uva 12169
/* 巨大的斐波那契数列_________________________________________________________________________________ #inclu ...
- Search Insert Position
int searchInsert(int* nums, int numsSize, int target) { ; ); ; int mid; while(low<=high){ mid=(lo ...
- [Thinking in Java]Nothing but Java
1 对象(Double Elephants) 1.1 抽象 万物皆对象.什么东西都可以抽象成属性和方法,达到可重用的目的. 对象之间交流靠发送消息.就是通过调用某个特定对象的方法来达到消息传递的目的. ...
- MongoDB学习笔记六:进阶指南
[数据库命令]『命令的工作原理』MongoDB中的命令其实是作为一种特殊类型的查询来实现的,这些查询针对$cmd集合来执行.runCommand仅仅是接受命令文档,执行等价查询,因此,> db. ...
- Spring集成jedis支持Redis3.0集群
接着上一节,我们通过spring FactoryBean实现redis 3.0集群JedisCluster与spring集成. http://www.linuxidc.com/Linux/2016- ...
- 自定义HttpModule的一些经验--配置篇
http://www.cnblogs.com/MyaSky/articles/2134954.html 自定义HttpModule的一些经验--配置篇 自定义web模块,需继承System.Web.I ...