2013-12-18 83 views
3

我有java.util.Date的initbinder:春@InitBinder的喬達時間

@InitBinder 
public void initBinder(WebDataBinder binder) { 
    SimpleDateFormat dateFormat = new SimpleDateFormat(
      Constants.DATE_PICTURE); 
    dateFormat.setLenient(false); 
    binder.registerCustomEditor(Date.class, new CustomDateEditor(
      dateFormat, true)); 

什麼可以一個用來綁定一個喬達時間LocalDateTime? 我正在使用Spring 3.1

回答

5

當您使用命令對象時,則可以使用完全不同的更優雅的方法:使用@DateTimeFormat註釋。

public class CommandObject { 
    @DateTimeFormat(pattern="yyyy/MM/dd hh:mm:ss a") 
    public Date myDate();   
} 

@Controller 
public class ControllerClass{ 
    @RequestMapping("/myUrl") 
    public ModelAndView(CommandObject c) {...} 
} 

(@see this blog對於某些例如格式化器)

+0

沒有必要對命令對象。註解可以用於任何可想象的方式;)+ 1爲沒有過時的PropertyEditor垃圾的解決方案。 – zeroflagL

+0

'@ DateTimeFormat'同樣支持Joda對象+1。 –