gpt4 book ai didi

从 2.4 升级后 Formatters.register 编译错误

转载 作者:行者123 更新时间:2023-12-04 03:21:17 25 4
gpt4 key购买 nike

当我运行“activator compile”时,从 2.4 升级到 2.5 后,我遇到了那些奇怪的“非静态”错误:

non-static method  <T>register(java.lang.Class<T>,play.data.format.Formatters.SimpleFormatter<T>) cannot be referenced from a static context

public MyController() {
Formatters.register(Xpto.class, new XptoConverter());
}

Xpto转换器:

public class XptoConverter extends SimpleFormatter<Xpto> {
@Override
public Xpto parse(String input, Locale l) throws ParseException {
return new Xpto().setId(Long.valueOf(input));
}

@Override
public String print(Xpto xpto, Locale l) {
return xpto.getId().toString();
}
}

最佳答案

方法不再是静态的;格式化程序被注入(inject)。参见 https://www.playframework.com/documentation/2.5.x/JavaForms#Register-a-custom-DataBinder .另见 https://github.com/playframework/playframework/pull/5734

你必须

  1. 创建 public class FormattersProvider implements Provider<Formatters> (进行转换)
  2. 创建 public class FormattersModule extends AbstractModule
  3. 启用FormattersModuleapplication.conf

关于从 2.4 升级后 Formatters.register 编译错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38189551/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com