2012-06-07 2 views
6

(java.util.Date 필드와 달리 DateTime 필드 용) 사용자 지정 포매터를 작성하려고하지만이 작업을 수행하는 데 어려움을 겪고 있습니다. AnnotationFormatter 클래스를 확장 할뿐만 아니라 내 주석을 만들었습니다. 응용 프로그램로드시 play.data.format.Formatters.register (DateTime.class, new MyDateTimeAnnotationFormatter())를 호출하지만 구문 분석 및 인쇄 메서드는 결코 트리거되지 않습니다.Play Framework 2.0 : 사용자 지정 포맷터

어떻게하면됩니까?

편집 : 문제의 코드가 도움이 될 수도,

크게 주석 클래스에 의해 영감을 주석 클래스 (재생 프레임 워크)에 포함) :

@Target({ FIELD }) 
@Retention(RUNTIME) 
@play.data.Form.Display(name = "format.datetime", attributes = { "pattern" }) 
public static @interface JodaDateTime { 
    String pattern(); 
} 

사용자 정의 포맷터 클래스 :

public static class AnnotationDateTimeFormatter extends AnnotationFormatter<JodaDateTime, DateTime> { 

    @Override 
    public DateTime parse(JodaDateTime annotation, String text, Locale locale) throws ParseException { 
     if (text == null || text.trim().isEmpty()) { 
      return null; 
     } 

     return DateTimeFormat.forPattern(annotation.pattern()).withLocale(locale).parseDateTime(text); 
    } 

    @Override 
    public String print(JodaDateTime annotation, DateTime value, Locale locale) { 
     if (value == null) { 
      return null; 
     } 

     return value.toString(annotation.pattern(), locale); 

    } 

포매터를 프레임 워크에 등록하려면이 클래스를 Application 클래스의 정적 인니 타이 저에서 호출해야합니다 (더 나은 장소를 배치 할 수 있습니다. 이 곳) 말해 주시기 :이 호출이 오류가 발생하지 않습니다, 그러나 여전히 포맷터에도 불구하고 실행되지 않도록 만들어 도착 디버거를 통해 단일 스테핑에 의해 확인했습니다

play.data.format.Formatters.register(DateTime.class, new AnnotationDateTimeFormatter()); 

다음과 같이 적절하게 DateTime 필드에 주석을 추가하십시오.

@Formats.JodaDateTime(pattern = "dd.MM.yyyy HH:mm:ss") 
public DateTime timeOfRequest = new DateTime(); 

나는 여기에 있습니다.

+0

코드를 게시 할 수 있습니까? – Somatik

+0

나는 정말로 할 수 있었다. :) 원래의 게시물을 수정하여 문제의 코드를 포함 시켰습니다. –

+0

해결 했습니까? – Denis

답변

0

DateTime 대신 JodaDateTime에 등록해야합니다.

play.data.format.Formatters.register(JodaDateTime.class, new AnnotationDateTimeFormatter()); 
+1

올바르지 않습니다. 'register'에 대한 첫번째 인자는'AnnotationFormatter.parse'에 의해 반환 된 타입이어야합니다. –

0

DateTime의 포맷터와 비슷한 문제가있었습니다. Global.onStart의 포맷터를 here과 같이 등록했습니다. 단순히 Global 클래스를 만들면 다시로드되지 않습니다. 재로드 (콘솔 출력에서 ​​--- (RELOAD) ---으로 표시)를 트리거 한 다른 파일을 수정하면 작동하기 시작합니다. 앱을 중지했다가 다시 시작해도 동일한 효과가 있어야합니다.