spring mvc提交日期类型参数
如题,spring mvc直接提交Date类型参数会报错,400 bad request的错误。在controller里加上
@InitBinder
protected void initBinder(HttpServletRequest request,ServletRequestDataBinder binder) throws Exception {
DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
CustomDateEditor dateEditor = new CustomDateEditor(format, true);
binder.registerCustomEditor(Date.class, dateEditor);
super.initBinder(request, binder);
}
或
@InitBinder
public void initBinder(WebDataBinder binder) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
dateFormat.setLenient(false);
binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));//true:允许输入空值,false:不能为空值
}
可以解决这个问题。但是这个时候Date类型的参数是null的话,还是会报错。
采用另外一种方式则更好,为null也不会报错,就是把请求参数封装为一个vo类,在对应的类属性上加上注解,这样
@DateTimeFormat(iso = ISO.DATE_TIME, pattern = "w:yyyy")
private Date startTime;
或者
@DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss")
private Date lastLoginDate;
另外如果使用验证框架,方法参数这样写(@Valid XxxParam param, BindingResult binding) ,就能直接通过BindingResult得到验证结果了。
spring mvc提交日期类型参数的更多相关文章
- spring mvc各种常见类型参数绑定方式以及json字符串绑定对象
在使用spring mvc作为框架的时候,为了规范,我们通常希望客户端的请求参数符合规范直接通过DTO的方式从客户端提交到服务端,以便保持规范的一致性,除了很简单的情况使用RequestParam映射 ...
- Spring mvc接受集合类型参数的方法
public String xxxxx(String xxxx, String xxxxx, @RequestParam("parameterList[]") List<St ...
- Spring MVC对日期处理的不友好问题
一.后台日期类型解析到前端 1.在springmvc的配置文件中添加这个.annotation-driven在配置文件中只配置一次 (此方法全局作用)<mvc:annotation-dr ...
- spring mvc 提交表单的例子
1. 构建MAVEN项目,然后转换成web格式,结构图如下: 2. 通过@RequestMapping来进行配置,当输入URL时,会以此找到对应方法执行,首先调用setupForm方法,该方法主要是生 ...
- spring mvc 提交数组等复杂类型
使用jquery提交,比如monthIncome的值是一个数组,在Java里用request.getParameterValues("monthIncome");取不到值,要这样才 ...
- spring mvc 提交表单汉字乱码
修改web.xml添加如下信息 <filter> <filter-name>characterEncodingFilter</filter-name> <fi ...
- spring mvc 复杂参数注入
过了这么久,又重新把博客拾起来了 来上海工作也已经有将近两周的时间了, 今天在整理项目的时候,遇到了一个关于参数注入的问题 背景: 我的开发前台用的是extjs4,在对后台spring mvc提交表单 ...
- Spring MVC与表单日期提交的问题
Spring MVC与表单日期提交的问题 spring mvc 本身并不提供日期类型的解析器,需要手工绑定, 否则会出现非法参数异常. org.springframework.beans.BeanIn ...
- Spring Mvc 传递参数要controller出现了400,日期参数全局处理,格式化yyyy-MM-dd 和yyyy-MM-dd HH:mm:ss
描述:今天做一个业务操作的时候,ajax传递参数要controller出现了400,前后台都没有报错. 问题:springmvc 在接收日期类型参数时,如不做特殊处理 会出现400语法格式错误 解决: ...
随机推荐
- systemtap学习笔记及疑问
http://blog.csdn.net/sunnybeike/article/details/7769663
- UNIX&Linux发展图谱
来自为知笔记(Wiz)
- IntelliJ IDEA 10.5.1 引用外部Jar包
具体步骤: File -> Project Structure (ctrl + shift + alt + s ) -> Module -> Dependencies -> A ...
- mysql 授权新的root用户
grant all privileges to *.* on system@'localhost' identified by 'woshishui' with grant option;
- shell脚本编写-自动部署及监控
1.编写脚本自动部署反向代理.web.nfs: I.部署nginx反向代理两个web服务,调度算法使用加权轮询 II.所有web服务使用共享存储nfs,保证所有web都对其有读写权限,保证数据一致性: ...
- SVG 签名动画 制作
不知道哪天看到的一个朋友圈里面有发的什么什么免费教签名之类的,就看了下SVG,做这功能还不错. 主要用到的几个属性,需要自行百度一下,不详说 stroke-dashoffset , stroke- ...
- 深度解析开发项目之 03 - enum的使用
深度解析开发项目之 03 - enum的使用 01 - 在#import和@interface之间定义typedef enum 注意: 默认是0,1,2,3 02 - 定义可以操作的数据类型的属性 0 ...
- ffmpeg api升级到3.3 api变化
void av_free_packet(AVPacket * pkt) Use void av_packet_unref(AVPacket * pkt) Wipe the packet. Unref ...
- OpenCV 入门示例之一:显示图像
前言 本文展示一个显示图像的示例程序,它用于从硬盘加载一副图像并在屏幕上显示. 代码示例 // 此头文件包含图像IO函数的声明 #include "highgui.h" int m ...
- 升级到Scriptcase 8.0的注意事项
Scriptcase的最新版本号v8.0已经正式公布了,因为国外下载较慢,我们在百度云盘创建了下载地址: http://pan.baidu.com/s/1qWwh6bi (Windows可运行文件)大 ...