javascript
java form的时间格式_SpringMvc接收日期表单提交,自动转换成Date类型方法
User中有birthday(Date)屬性,用戶注冊的時候,選擇日期即可,然后提交表單,可spring mvc 報錯,400 Bad Request意思是不能把字符串轉(zhuǎn)為Date類型的。
實體類中加日期格式化注解@DateTimeFormat(pattern = "yyyy-MM-dd")
private Date birthday;控制器Controller中加入一段數(shù)據(jù)綁定代碼//將字符串轉(zhuǎn)換為Date類
@InitBinder
public void initBinder(WebDataBinder binder, WebRequest request) {
//轉(zhuǎn)換日期格式
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
// 注冊自定義的編輯器
binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
}
方法三:實現(xiàn)一個全局日期類型轉(zhuǎn)換器并進(jìn)行配置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
//轉(zhuǎn)換日期格式
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
}
}
//并在spingMVC配置文件進(jìn)行配置
方法四:jsp頁面配置或Freemark中配置
總結(jié)
以上是生活随笔為你收集整理的java form的时间格式_SpringMvc接收日期表单提交,自动转换成Date类型方法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 3D 空间音效+空气衰减+人声模糊
- 下一篇: 计算方法 matlab,计算方法及其MA