2015-01-20 12 views
1

내 애플리케이션에서 Play 2.3.7을 사용 중입니다.Play 2의 고유 한 양식에 포맷터를 적용하는 방법은 무엇입니까?

나는 날짜를 특별한 포매터를 필요로하는 형태를 갖는다. 그래서보기를 양식을 생성하고 렌더링 내 컨트롤러의 방법에서이 작업을 수행 :

Formatters.register(Date.class, new SimpleFormatter<Date>() { 
    private SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy"); 

    @Override 
    public Date parse(String input, Locale locale) throws ParseException { 
     try { 
      return formatter.parse(input); 
     } catch (ParseException e) { 
      // We catch the error because it should be transparent for the user 
      Logger.error(e.getLocalizedMessage(), e); 
      return null; 
     } 
    } 

    @Override 
    public String print(Date input, Locale locale) { 
     return formatter.format(input); 
    } 
}); 

문제는이 포매터 내 모든 응용 프로그램에 전 세계적으로 적용되는 것입니다. 그리고 다른 양식은 다른 날짜 형식을 사용하고 있습니다 : dd/MM/yyyy HH : mm : ss (프랑스어 형식). 그래서 한 포맷에만 포맷터를 적용하거나 적어도 포맷터 등록을 취소 할 수 있습니까?

감사

답변

1

당신은 Formats 주석 중 하나를 사용하고 다음과 같이 양식 필드 주석을 달 수 있습니다 :

public class SampleForm { 

    @Formats.DateTime(pattern = "yyyy-MM-dd HH:mm:ss") 
    private Date date; 

    ... 

} 
+0

덕분에, 내 문제에 대한 작업을 수행합니다. 그러나이 질문은 전체 응용 프로그램의 형식이 아닌 한 형식에 대해서만 포맷터를 적용하려는 사람에게도 적용됩니다. – c4k