Spring MVC中的日期 [英] Date in Spring MVC

查看:90
本文介绍了Spring MVC中的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在我的JSP页面中使用 type = Date 将日期解析为控制器。但是我在语法上得到了不正确的数据(400错误)。我尽力找到解决方案,但没有找到合适的解决方案。请帮我解决这个问题。任何建议将不胜感激。

I am trying to use type=Date in my JSP page to parse the date to the controller. But I am getting syntactically incorrect data (400 error). I tried my best to find the solution but failed to get the proper one. Please help me to solve this. Any suggestions would be appreciated.

我的域类别:

public class Doctor{  
     @DateTimeFormat(pattern = "MM-DD-YYYY")
     private Date dateOfBirth=null;

     // Setter and Getter
}

JSP代码:

<f:input path="dateOfBirth" type="Date"/>

我也尝试在contoller中使用@InitBinder,但无法成功。

I tried using the @InitBinder in contoller as well, but could not succeed.

推荐答案

我尝试运行您的代码,但发生异常。原因来自jsp页面上的日期格式与java bean上的日期格式不同。
让我们更改此格式

I try to run your code and an exception happens. The cause comes from where date format on jsp page is not same as format on java bean. Let's change this format

@DateTimeFormat(pattern = "MM-DD-YYYY")
private Date dateOfBirth=null;

@DateTimeFormat(pattern="yyyy-MM-dd") 
private Date dateOfBirth=null;

然后解决此问题

这篇关于Spring MVC中的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆