java lang reflect invocation,Spring數據轉換(二)-- @InitBinder

 2023-11-18 阅读 29 评论 0

摘要:Spring支持Jdk提供的PropertyEditor實現數據類型的轉換。相比于Spring提供的數據轉換,PropertyEditor的使用相對簡單,不需要在xml文件中配置什么。 首先繼承PropertyEditorSupport實現其中的setAsText方法 public class DateEditor extends PropertyEditorSuppo

Spring支持Jdk提供的PropertyEditor實現數據類型的轉換。相比于Spring提供的數據轉換,PropertyEditor的使用相對簡單,不需要在xml文件中配置什么。

首先繼承PropertyEditorSupport實現其中的setAsText方法

public class DateEditor extends PropertyEditorSupport {// 將傳如的字符串數據轉換成Date類型@Overridepublic void setAsText(String text) throws IllegalArgumentException {SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");try {Date date = dateFormat.parse(text);setValue(date);} catch (ParseException e) {e.printStackTrace();}}
}

同時在Controller中添加如下的代碼:

// 在控制器初始化時注冊屬性編輯器
@InitBinder
public void initBinder(WebDataBinder binder){// 注冊自定義編輯器,將方法傳入的Dateleixng 使用 DateEditor進行轉換binder.registerCustomEditor(Date.class, new DateEditor());
}

java lang reflect invocation。@InitBinder會在控制器初始化時注冊屬性編輯器,WebDataBinder對象用于處理請求消息和處理方法的綁定工作。

轉載于:https://www.cnblogs.com/arax/p/8551839.html

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/3/174079.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息