简体   繁体   English

Play Framework 2.0:自定义格式化程序

[英]Play Framework 2.0: Custom formatters

I'm trying to write a custom formatter (for DateTime fields, as opposed to java.util.Date fields), but am having a hard time getting this to work. 我正在尝试编写一个自定义格式化程序(对于DateTime字段,而不是java.util.Date字段),但我很难让它工作。 I've created my annotation, as well as extended the AnnotationFormatter class. 我已经创建了我的注释,并扩展了AnnotationFormatter类。 I call play.data.format.Formatters.register(DateTime.class, new MyDateTimeAnnotationFormatter()) on Application load, but the parse and print methods never trigger. 我在Application load上调用了play.data.format.Formatters.register(DateTime.class,new MyDateTimeAnnotationFormatter()),但是解析和print方法永远不会触发。

How am I supposed to do this? 我该怎么做?

Edit: the code in question might be helpful ;) 编辑:有问题的代码可能会有所帮助;)

The annotation class (heavily inspired by the annotation class included with Play Framework): 注释类(受Play Framework附带的注释类的启发):

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

The custom formatter class: 自定义格式化程序类:

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);

    }

To register the formatter with the framework, I make this call in a static initalizer on the Application class (there might very well be a better place to put this, feel free to tell me where): 要在框架中注册格式化程序,我在Application类的静态initalizer中进行此调用(可能有一个更好的地方放置它,随时告诉我在哪里):

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

I've confirmed by single-stepping through the debugger that this call gets made and that no errors are thrown, yet still the formatter isn't run in spite of annotating the DateTime fields appropriately like this: 我已经通过调试器单步执行确认此调用已经完成并且没有抛出任何错误,但是仍然运行格式化程序,尽管如此适当地注释DateTime字段:

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

I'm at loss here. 我在这里失落。

您需要注册JodaDateTime而不是DateTime。

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

I had a similar problem with a formatter for DateTime . 我对DateTime的格式化程序有类似的问题。 I was registering the formatter from my Global.onStart as described here . 我是从我的注册格式化Global.onStart描述这里 It appears simply creating the Global class didn't trigger a reload. 看起来简单地创建Global类并没有触发重新加载。 Once I modified another file which triggered a reload (shown as --- (RELOAD) --- in the console output) it started working. 一旦我修改了另一个触发重载的文件--- (RELOAD) ---在控制台输出中显示为--- (RELOAD) --- )它就开始工作了。 Stopping and restarting your app should have the same effect. 停止并重新启动您的应用应具有相同的效果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM