日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

springMVC 前后台日期格式传值解决方式之二(共二) @InitBinder的使用

發布時間:2025/3/15 c/c++ 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 springMVC 前后台日期格式传值解决方式之二(共二) @InitBinder的使用 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

關于springmvc日期問題的解決方式 除了本博客的【springMVC 前后臺日期格式傳值解決方式之 @DateTimeFormat的使用和配置】一文,

還有如下這種方式:

在Controller里加上這段代碼:

1 @InitBinder 2 public void initBinder(ServletRequestDataBinder binder) { 3 /** 4 * 自動轉換日期類型的字段格式 5 */ 6 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); 7 binder.registerCustomEditor(Date.class, new CustomDateEditor(sdf, true)); 8 9 10 }

注意,如果前臺有多重日期格式,寫成類似下面的方式是沒有什么卵用的

1 @InitBinder 2 public void initBinder(ServletRequestDataBinder binder) { 3 /** 4 * 自動轉換日期類型的字段格式 5 */ 6 SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd"); 7 SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy年MM月"); 8 try { 9 binder.registerCustomEditor(Date.class, new CustomDateEditor(sdf2, true)); 10 }catch(Exception e) { 11 binder.registerCustomEditor(Date.class, new CustomDateEditor(sdf1, true)); 12 } 13 14 }

?

怎么解決呢?

可參考這個問題:http://bbs.csdn.net/topics/380055180

?

原文內容如下

----------------------------------------------------------分界線開始-----------------------------------------------------------

配置文件為

1 2 3 4 5 <bean?class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">?? ????<property?name="webBindingInitializer">?? ????????<bean?class="xx.xxx.MyBindingInitializer"/>?? ????</property>?? </bean>?



MyBindingInitializer中,initBinder方法里的
binder.registerCustomEditor(Date.class,?new?XXXEditor());


在XXXEditor的setAsText方法中,使用系統所有可能用到的format格式一一嘗試,捕獲異常,最后正確綁定。

-------------------------------------------------------分界線結束----------------------------------------------------------------

?

至于其中的XXXEditor怎么寫,大家可以參照例子中的CustomDateEditor,即:org.springframework.beans.propertyeditors.CustomDateEditor源碼中怎么寫的。


轉載于:https://www.cnblogs.com/flying607/p/4691714.html

總結

以上是生活随笔為你收集整理的springMVC 前后台日期格式传值解决方式之二(共二) @InitBinder的使用的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。