【问题标题】:Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' for property 'date_added无法将类型“java.lang.String”的属性值转换为属性“date_added”所需的类型“java.util.Date”
【发布时间】:2020-09-21 06:44:18
【问题描述】:

模型类

@实体 公共类监视列表 {

    @Id
    private String scrip_name;
    @Temporal(TemporalType.DATE)
    @DateTimeFormat(pattern = "MM/dd/yyyy")
    private Date date_added; 
    private double recom_price;  // recommended price
    private double stop_loss;
    private double curr_price;   // current price
    private double lmar;  // low made after recommended price
    Getters and Setters and toostring   
}

服务类

public interface WatchlistService {
    
    Watchlist saveWatchlist(Watchlist watchlist);
    void deleteWatchlist(Watchlist watchlist);
    List<Watchlist> getAllWatchlist();

}

块引用 控制器

@控制器 公共类 WatchlistController {

    @InitBinder
    public void initBinder(WebDataBinder webDataBinder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
        webDataBinder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
    }

    @Autowired
    WatchlistService service;
    
    @RequestMapping("/createWatchlist")
    public String showcreate() {
        return "createWatchlist";
    }
    
    @RequestMapping("/savewatchlist")
    public String savewatchlist(@ModelAttribute("watchlist") Watchlist watchlist, ModelMap modelMap) 
{
        Watchlist watchlistSaved = service.saveWatchlist(watchlist);
        String msg = watchlistSaved.getScrip_name(); 
        modelMap.addAttribute("msg", msg);
        return "createWatchlist";
    }
}

我正在尝试从 jsp 获取日期并插入到 mysql 中

下面是jsp文件中的sn-p代码

Date Added:<input type="date" name="date_added"/>

【问题讨论】:

  • 请在您的问题中添加一些详细信息,例如异常及其堆栈跟踪。
  • 确保您从 JSP 获得的日期格式与您在此处声明的 SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy"); 相同,否则共享异常和堆栈跟踪。
  • 字段 'date_added' 上的对象 'watchlist' 中的字段错误:拒绝值 [2020-09-19];代码 [typeMismatch.watchlist.date_added,typeMismatch.date_added,typeMismatch.java.util.Date,typeMismatch];参数 [org.springframework.context.support.DefaultMessageSourceResolvable: 代码 [watchlist.date_added,date_added];论据 [];默认消息 [date_added]];默认消息 [无法将类型“java.lang.String”的属性值转换为属性“date_added”所需的类型“java.util.Date”;嵌套异常是 java.lang.IllegalArgumentException:无法解析日期:无法解析日期:]
  • 尝试像这样传递字段webDataBinder.registerCustomEditor(Date.class,"date_added", new CustomDateEditor(dateFormat, true));
  • 如果这不起作用然后更改:@DateTimeFormat(pattern = "MM/dd/yyyy") 到 @DateTimeFormat(pattern = "yyyy-MM-dd")

标签: spring-boot hibernate


【解决方案1】:

从 cmets 部分日志中,异常与日期解析有关。

嵌套异常是 java.lang.IllegalArgumentException: 不能 解析日期:无法解析的日期:]

看起来来自 jsp 的日期字符串不是 initBinder 中定义的格式。

您需要确保格式相同。

只是为了测试目的,将后端日期格式更改为与jsp相同并检查它是否有效。

您发送的格式是 2020-09-19

initBinder 中的格式为 MM/dd/yyyy 09-19-2020

显然它不会起作用。更改任一侧的格式即可。

【讨论】:

  • 它已经存在 @InitBinder public void initBinder(WebDataBinder webDataBinder) { SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy"); webDataBinder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true)); }
  • 它已经存在于控制器中,我需要更改其他任何内容吗 @InitBinder public void initBinder(WebDataBinder webDataBinder) { SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy"); webDataBinder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true)); }
猜你喜欢
  • 2018-08-21
  • 2017-05-06
  • 2015-03-29
  • 2021-06-26
  • 2014-07-05
  • 2015-04-28
  • 1970-01-01
  • 2021-06-13
  • 1970-01-01
相关资源
最近更新 更多