春天@RequestParam日期格式 [英] Spring @RequestParam Date Formatting

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

问题描述

你如何格式化传入 @RequestParam 使用注释?形式发送的MM / DD / YYYY格式和控制器中的日期不拿起它作为一个有效的日期

  @RequestMapping(PARAMS =/ updateDate)
公共字符串@ResponseBody updateDate(HttpServletRequest的请求
        ,@RequestParam整数的productId
        ,@RequestParam日期dateReceived){
    // code这里...
}


解决方案

使用Spring的注解<一个href=\"http://docs.spring.io/autorepo/docs/spring/4.0.2.RELEASE/javadoc-api/org/springframework/format/annotation/DateTimeFormat.ISO.html\"相对=nofollow> DateTimeFormat

  @RequestMapping(PARAMS =/ updateDate)
公共字符串@ResponseBody updateDate(HttpServletRequest的请求
        ,@RequestParam整数的productId
        ,@RequestParam @DateTimeFormat(模式=MM / DD / YYYY)日期dateReceived){
    // code这里...
}

How do you format the incoming @RequestParam using annotations? The form is sending the date in MM/DD/YYYY format and the controller is not picking it up as a valid Date.

@RequestMapping(params="/updateDate")
public @ResponseBody String updateDate(HttpServletRequest request
        , @RequestParam Integer productId
        , @RequestParam Date dateReceived) {
    // Code here...
}

解决方案

Use the Spring annotation DateTimeFormat

@RequestMapping(params="/updateDate")
public @ResponseBody String updateDate(HttpServletRequest request
        , @RequestParam Integer productId
        , @RequestParam @DateTimeFormat(pattern="MM/dd/yyyy") Date dateReceived) {
    // Code here...
}

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

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