spring mvc date format with form:input
我有hibernate实体和bean:
@Entity public class GeneralObservation { @DateTimeFormat(pattern = "dd/MM/yyyy") Date date; @Column public Date getDate() { return date; } public void setDate(Date date) { this.date = date; } }
我也有
@InitBinder protected void initBinder(WebDataBinder binder) { SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy"); binder.registerCustomEditor(Date.class, new CustomDateEditor( dateFormat, false)); }
和
form:input id = "datepicker" name="date" itemLabel="date" path="newObservation.date"
当我转到我的url时,我看到:
如何强制它具有mm / DD / yyyy格式? 感谢名单
您可以使用fmt:formatDate jstl标记:
解决方案是放
到mvc-dispatcher-servlet.xml以及xmlns:mvc =“http://www.springframework.org/schema/mvc”到xml文件的开头。
在我的代码中,我以这种方式使用binder:
@InitBinder public void initBinder(WebDataBinder binder) { SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy"); dateFormat.setLenient(false); binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true)); }
在HTML5中有输入类型=“日期”,在Spring中也是如此(Chrome,Opera和我这个Safary也是如此,但在Firefox中还没有