springmvc 接受特殊类型字段的处理方法
springmvc接受前台传入的数据时如果该字段类型无法被封装(如Date),则会出现400 Bad Request错误,解决方法如下。
1.在需要处理的字段前加上注解:
@DateTimeFormat( pattern = "yyyy-MM-dd" )
然后在项目中引入joda-time.jar包,最后在在 SpringMVC 配置 xml 文件中中加入配置: <mvc:annotation-driven /> 。这一句配置是一种简写,其实是给 spring 容 器中注入了两个 Bena ,分别是: DefaultAnnotationHandlerMapping 和 AnnotationMethodHandlerAdapter 。 @DateTimeFormat 注解的内部同样需要使用到前面注入的两 个 bean 去处理,所以缺少这个配置, Spring 容器中没有对应的 bean 去处理注解同样也会报错。至此,所有的步骤都完成了,可以跑了。、
2.使用springmvc提供的@InitBinder标签:
在控制层中加入一个编辑器
/**
* 设置控制层特殊字段转换规则(如 Date),可重载
* @param binder
*/
@InitBinder
protected void initBinder(WebDataBinder binder) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
dateFormat.setLenient(false);
binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
}
spring提供了大量的实现类,诸如CustomDateEditor ,CustomBooleanEditor,CustomNumberEditor等
@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());
}
也可以不使用自带这些编辑器类,通过继承PropertiesEditor实现自定义编辑类
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();
}
}
3:配置全局日期转换器。
http://blog.csdn.net/chenleixing/article/details/45156617
springmvc 接受特殊类型字段的处理方法的更多相关文章
- springMVC接受json类型数据
springMVC接受json格式的数据很简单 使用@RequestBody 注解,标识从请求的body中取值 服务端示例代码 @RequestMapping(value = "/t4&qu ...
- SpringMVC处理Date类型的成员变量方法
原文链接:http://www.tuicool.com/articles/aYfaqa 在使用 SpringMVC 的时候,我们可能需要将一个对象从 View 传递给 Controller .而当这个 ...
- Java 存储和读取 oracle CLOB 类型字段的实用方法
import java.io.BufferedReader; import java.io.IOException; import java.io.Reader; import java.io.Str ...
- springmvc 1.接受日期类型的参数 2.后台返回json串的格式处理(返回json串null值处理为"")
springmvc中的配置: <bean id="dateConvert" class="com.iomp.util.DateConvert"/> ...
- SQL Server中TEXT类型字段值在数据库中追加字符串方法
在数据上我们往往会遇到ntext大文本类型,这种类型如果和 nvarchar类型相加会出现问题,所以有一中方法可以解决这种问题. 使用的sql 函数: TEXTPTR:返回要更新的 text.nt ...
- C# WebAPI中DateTime类型字段在使用微软自带的方法转json格式后默认含T的解决办法
原文:C# WebAPI中DateTime类型字段在使用微软自带的方法转json格式后默认含T的解决办法 本人新手,在.Net中写WebAPI的时候,当接口返回的json数据含有日期时间类型的字段时, ...
- python_way day18 html-day4, Django路由,(正则匹配页码,包含自开发分页功能), 模板, Model(jDango-ORM) : SQLite,数据库时间字段插入的方法
python_way day18 html-day4 1.Django-路由系统 - 自开发分页功能 2.模板语言:之母板的使用 3.SQLite:model(jDango-ORM) 数据库时间字 ...
- SpringMVC接受JSON参数详解及常见错误总结我改
SpringMVC接受JSON参数详解及常见错误总结 最近一段时间不想使用Session了,想感受一下Token这样比较安全,稳健的方式,顺便写一个统一的接口给浏览器还有APP.所以把一个练手项目的前 ...
- SpringMVC 处理Date类型数据@InitBinder @DateTimeFormat 注解 的使用
使用SpringMVC的时候,需要将表单中的日期字符串转换成对应JavaBean的Date类型,而SpringMVC默认不支持这个格式的转换,解决方法有两种,如下: 方法一 . 在需要日期转换的Con ...
随机推荐
- 关于MySQL数据导出导入
工具 mysqlmysqldump 应用举例 导出 导出全库备份到本地的目录 mysqldump -u$USER -p$PASSWD -h127.0.0.1 -P3306 --routines --d ...
- Hbuilder与svn快速连接并在手机上测试页面
大家好,今天讲一下Hbuilder怎样与svn连接在一起,并且在移动端上面做真是的页面测试. 1,打开Hbuilder软件,在工具中,安装插件,找到svn插件安装. 2.点击文件,导入,从svn中检测 ...
- csv 文件用Excel打开乱码
最近在做一个上传的功能,记录下自己的感受. 刚开始我用的是excel上传:但是发现客户服务器有用64位的,使用的时候程序会报错: 未在本地计算机上注册“Microsoft.Jet.OLEDB.4.0” ...
- xxxxxxx
int c; int b; bool h;
- DB2 claim与drain
DB2使用claim & drain来控制SQL与utility对数据库对象的访问. claim说明有对象正在访问或是将要访问该对象,而drain则是在对象上面加一个drain lock,等所 ...
- linux 几个控制流语句的格式例子(if语句)
linux 几个控制流语句的格式例子:if 语句例子:#!/bin/sh a=10b=20 if [ $a == $b ]then echo "a is equal to b"el ...
- javascript标识符
标识符,就是指变量.函数.属性的名字,或者函数的参数. 规则 1.第一个字符必须是一个字母.下划线或是美元符号($) 2.其他字符可以是字母.下划线.美元符号或数字 3.不能是关键字和保留字 4.区分 ...
- coffeeScript中类的多态[学习篇]
类的一大应用就是多态.多态是一个面向对象编程的高级术语----“一个东西可编程很多不同的东西,但不是任何东西”.[引自coffeescript深入浅出] class Shape constructor ...
- coffeeScript中类的继承[学习篇]
只是在看深入浅出coffeescript中感觉真的很好,不光是coffe写法简单,生成的js也值得学习,废话不多说了,直接抄个书上的例子 class Pet constructor: -> @i ...
- Android模拟器连接本李服务器localhost
Android模拟(simulator)把它自己作为了localhost,也就是说,代码中使用 localhost或者127.0.0.1来访问,都是访问模拟器自己! 如果你想在模拟器simulator ...