spring json日期到java日期对象
在我的 Spring[3.0] 应用程序中,
我想使用 jquery.ajax 提交数据。 Spring 正在解释此请求并转换为适当的对象。但是,它无法解析日期属性。如何将它们从 JSON 请求传递到 java?请帮忙...
public class MyUser{
public String name;
public Date birth;
}
@RequestMapping("/app/saveuser")
public String save(@ModelAttribute MyUser user)
{
//User has NO date.. :(
}
Front end code...
jQuery.post("/app/saveuser",
{name:"myName", birth:new Date()},
function(data){
alert(data);
} );
In my Spring[3.0] application,
I want to submit data using jquery.ajax. Spring is interpreting this request and converting to appropriate Object. however, it is unable to parse the date attributes. How do I pass them from my JSON request to java? please help...
public class MyUser{
public String name;
public Date birth;
}
@RequestMapping("/app/saveuser")
public String save(@ModelAttribute MyUser user)
{
//User has NO date.. :(
}
Front end code...
jQuery.post("/app/saveuser",
{name:"myName", birth:new Date()},
function(data){
alert(data);
} );
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
日期不能绑定到命令对象,除非您指定可以读取某些日期格式的 PropertyEditor 或转换服务(这是设计上的限制,因为您无法在不知道格式的情况下判断“01/02/03”的含义);尝试编写一些
@InitBinder
方法,并为您期望的格式注册一个CustomDateEditor
。Dates cannot be bound to a command object unless you specify a PropertyEditor or a Conversion service that can read some date format (this is a limitation by design, as you cannot tell what '01/02/03' means without knowing the format); try to write some
@InitBinder
method and there register aCustomDateEditor
for the format you're expecting.