package com.chentianyu.controller;import org.springframework.beans.propertyeditors.CustomDateEditor;import org.springframework.format.annotation.DateTimeFormat;import org.springframework.stereotype.Controller;import org.springframework.web.bind.WebDataBinder;import org.springframework.web.bind.annotation.InitBinder;import org.springframework.web.bind.annotation.RequestMapping;import java.text.SimpleDateFormat;import java.util.Date;@Controllerpublic class MyDateAction {SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");//单个日期注入/*** 日期:<input type="date" name="myDate" /><br>* @return*//*@RequestMapping("/myDate")public String myDate(@DateTimeFormat(pattern = "yyyy-MM-dd"*//*将字符串转为日期注入进去*//*) Date myDate){System.out.println(myDate);System.out.println(sf.format(myDate));return "show";}*///全局日期处理//注册一个全局的日期处理注解@InitBinderpublic void initBinder(WebDataBinder dataBinder/*重点*/){dataBinder.registerCustomEditor(/*转化的类型*/Date.class,/*使用什么工具去转*/new CustomDateEditor(sf,true));}@RequestMapping("/myDate")public String myDate(Date myDate){System.out.println(myDate);System.out.println(sf.format(myDate));return "show";}}
好处是连<mvc:annotation-driven />都省去可以不写
