(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();
나는 여기에 있습니다.
코드를 게시 할 수 있습니까? – Somatik
나는 정말로 할 수 있었다. :) 원래의 게시물을 수정하여 문제의 코드를 포함 시켰습니다. –
해결 했습니까? – Denis