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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

java form的时间格式_SpringMvc接收日期表单提交,自动转换成Date类型方法

發布時間:2023/12/10 javascript 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java form的时间格式_SpringMvc接收日期表单提交,自动转换成Date类型方法 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

User中有birthday(Date)屬性,用戶注冊的時候,選擇日期即可,然后提交表單,可spring mvc 報錯,400 Bad Request意思是不能把字符串轉為Date類型的。

實體類中加日期格式化注解@DateTimeFormat(pattern = "yyyy-MM-dd")

private Date birthday;控制器Controller中加入一段數據綁定代碼//將字符串轉換為Date類

@InitBinder

public void initBinder(WebDataBinder binder, WebRequest request) {

//轉換日期格式

DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

// 注冊自定義的編輯器

binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));

}

方法三:實現一個全局日期類型轉換器并進行配置package nuc.ss.wlb.core.web;

import java.text.DateFormat;

import java.text.SimpleDateFormat;

import java.util.Date;

import org.springframework.beans.propertyeditors.CustomDateEditor;

import org.springframework.web.bind.WebDataBinder;

import org.springframework.web.bind.support.WebBindingInitializer;

import org.springframework.web.context.request.WebRequest;

public class CustomDateEdtor implements WebBindingInitializer {

public void initBinder(WebDataBinder binder, WebRequest request) {

// TODO Auto-generated method stub

//轉換日期格式

DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));

}

}

//并在spingMVC配置文件進行配置

方法四:jsp頁面配置或Freemark中配置

總結

以上是生活随笔為你收集整理的java form的时间格式_SpringMvc接收日期表单提交,自动转换成Date类型方法的全部內容,希望文章能夠幫你解決所遇到的問題。

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