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 ...
随机推荐
- CSS背景色渐变
试了下 渐变色 ,主要确定开始位置 结束位置,以及对应的color-stop; 以下是兼容不同浏览器的代码片段 代码:<style type="text/css& ...
- Python requests 为pfsense 添加Routes
# !/usr/bin/python 2 # -*- coding: utf-8 -*- __author__ = "Evilxr" import requests ips = o ...
- 跳过IE10安装VS2013
@ECHO OFF :IE10HACK REG ADD "HKLM\SOFTWARE\Wow6432Node\Microsoft\Internet Explorer" /v Ver ...
- js中event.target
event.srcElement从字面上可以看出来有以下关键字:事件,源 他的意思就是:当前事件的源, 我们可以调用他的各种属性 就像:document.getElementById(&quo ...
- Python之路,day9-Python基础
回顾:抽象方法@staticmethod 不能访问类的任何属性@classmethod 类方法 只能访问公有属性@property 属性方法 , 把一个方法变成一个静态属性def sayhi() pa ...
- SQL 用户定义表类型,在存储过程里使用表类型,表参数作参数
.定义表类型SUTDENTTYPE,包含三个字段,分别对应学生表的NAME,SEX和PHONE.之所以如此创建,我是准备在插入新学生数据的存储过程中,以它为参数. GO CREATE TYPE S ...
- Tcp方式采集CNC兄弟设备数据
先说下为了采集CNC兄弟设备的数据可谓是一波三折. 因为首次接触brother设备(CNC)是直接在设备上设置IP.用户名.密码,然后直连PC,用Ftp可以查看和下载CNC brother设备里的数据 ...
- Python 各进制间的转换(转)
转载自:http://blog.chinaunix.net/uid-21516619-id-1824975.html python 2.6以后内置函数#10进制转为2进制>>> bi ...
- koa知识点
1. Koa 不支持 直接调用底层 res 进行响应处理.请避免使用以下 node 属性: res.statusCode res.writeHead() res.write() res.end() 2 ...
- Apache Tomcat 7 安装与配置
下载 首先需要下载tomcat7的安装文件,地址如下: http://mirror.bit.edu.cn/apache/tomcat/tomcat-7/v7.0.69/bin/apache-tomca ...