Play Framework 2.0:自定义格式化程序 [英] Play Framework 2.0: Custom formatters

查看:147
本文介绍了Play Framework 2.0:自定义格式化程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写自定义格式化程序(对于DateTime字段,而不是java.util.Date字段),但我很难让它工作。我已经创建了我的注释,并扩展了AnnotationFormatter类。我在应用程序加载时调用了play.data.format.Formatters.register(DateTime.class,new MyDateTimeAnnotationFormatter()),但是解析和打印方法永远不会触发。

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. I've created my annotation, as well as extended the AnnotationFormatter class. I call play.data.format.Formatters.register(DateTime.class, new MyDateTimeAnnotationFormatter()) on Application load, but the parse and print methods never trigger.

如何我应该这样做吗?

编辑:有问题的代码可能会有所帮助;)

the code in question might be helpful ;)

注释class(受Play Framework附带的注释类的启发):

The annotation class (heavily inspired by the annotation class included with Play Framework):

@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类的静态initalizer中(可能有一个更好的地方放这个,随时告诉我在哪里):

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

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

我已通过调试器单步执行此调用确认并且没有错误抛出,但格式化程序仍未运行,尽管如下所示正确地注释DateTime字段:

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:

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

我在这里亏损。

推荐答案

我对 DateTime 的格式化程序有类似的问题。我按照 Global.onStart 注册格式化程序>此处。看来只是创建全局类没有触发重新加载。一旦我修改了另一个触发重新加载的文件(在控制台输出中显示为 ---(RELOAD)--- ),它就开始工作了。停止并重新启动您的应用应具有相同的效果。

I had a similar problem with a formatter for DateTime. I was registering the formatter from my Global.onStart as described here. It appears simply creating the Global class didn't trigger a reload. Once I modified another file which triggered a reload (shown as --- (RELOAD) --- in the console output) it started working. Stopping and restarting your app should have the same effect.

这篇关于Play Framework 2.0:自定义格式化程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆