【问题标题】:Can I pass a Java Interface to a Play! template我可以将 Java 接口传递给 Play!模板
【发布时间】:2014-07-16 21:53:01
【问题描述】:

我有一个 scala 模板,我传入一个 Java List<SomeConcreteClass>,它工作得很好。我目前面临的挑战是,我需要这个模板不仅可以渲染 SomeConcreteClass 的实例,还可以渲染任何实现 Java 接口的类。

我已经创建了一个界面:

public interface MyInterface { /* bunch of method signatures here */ }

并创建了我的 scala 模板的另一个版本(称为 myNewInterfaceTemplate.scala.html)来获取接口:模板的顶部如下所示:

@(myInterface: List[MyInterface], showColConfig: Boolean)

然后我创建一个新的控制器动作来呈现与此类似的模板:

public static Result newRenderPolymorphically() {
  List<MyInterface> items = 
  SomeOtherClass.findAllActiveItems(ClassThatImplementsMyInterface.class);
return ok(myNewInterfaceTemplate.render(items, false));

}

然后向新操作添加一个类似于以下内容的路由:

GET     /newRenderPolymorphically                  controllers.newRenderPolymorically()

当我使用新的模板、界面和操作编译项目时,我得到的错误并不指向我的代码中的任何内容:

java.lang.NullPointerException
    at javassist.bytecode.stackmap.Tracer.doASTORE(Tracer.java:453)
    at javassist.bytecode.stackmap.Tracer.doOpcode54_95(Tracer.java:330)
    at javassist.bytecode.stackmap.Tracer.doOpcode(Tracer.java:98)
    at javassist.bytecode.stackmap.MapMaker.make(MapMaker.java:183)
    at javassist.bytecode.stackmap.MapMaker.make(MapMaker.java:193)
    at javassist.bytecode.stackmap.MapMaker.make(MapMaker.java:193)
    at javassist.bytecode.stackmap.MapMaker.make(MapMaker.java:142)
    at javassist.bytecode.stackmap.MapMaker.make(MapMaker.java:97)
    at javassist.bytecode.MethodInfo.rebuildStackMap(MethodInfo.java:417)
    at javassist.bytecode.MethodInfo.rebuildStackMapIf6(MethodInfo.java:399)
    at javassist.expr.ExprEditor.doit(ExprEditor.java:113)
    at javassist.CtBehavior.instrument(CtBehavior.java:679)
    at play.core.enhancers.PropertiesEnhancer.rewriteAccess(PropertiesEnhancer.java:152)
    at sbt.PlayCommands$$anonfun$PostCompile$1$$anonfun$apply$31.apply(PlayCommands.scala:326)
    at sbt.PlayCommands$$anonfun$PostCompile$1$$anonfun$apply$31.apply(PlayCommands.scala:326)
    at scala.collection.mutable.ResizableArray$class.foreach(ResizableArray.scala:60)
    at scala.collection.mutable.ArrayBuffer.foreach(ArrayBuffer.scala:47)
    at sbt.PlayCommands$$anonfun$PostCompile$1.apply(PlayCommands.scala:326)
    at sbt.PlayCommands$$anonfun$PostCompile$1.apply(PlayCommands.scala:305)
    at sbt.Scoped$$anonfun$hf7$1.apply(Structure.scala:583)
    at sbt.Scoped$$anonfun$hf7$1.apply(Structure.scala:583)
    at scala.Function1$$anonfun$compose$1.apply(Function1.scala:49)
    at sbt.Scoped$Reduced$$anonfun$combine$1$$anonfun$apply$12.apply(Structure.scala:311)
    at sbt.Scoped$Reduced$$anonfun$combine$1$$anonfun$apply$12.apply(Structure.scala:311)
    at sbt.$tilde$greater$$anonfun$$u2219$1.apply(TypeFunctions.scala:41)
    at sbt.std.Transform$$anon$5.work(System.scala:71)
    at sbt.Execute$$anonfun$submit$1$$anonfun$apply$1.apply(Execute.scala:232)
    at sbt.Execute$$anonfun$submit$1$$anonfun$apply$1.apply(Execute.scala:232)
    at sbt.ErrorHandling$.wideConvert(ErrorHandling.scala:18)
    at sbt.Execute.work(Execute.scala:238)
    at sbt.Execute$$anonfun$submit$1.apply(Execute.scala:232)
    at sbt.Execute$$anonfun$submit$1.apply(Execute.scala:232)
    at sbt.ConcurrentRestrictions$$anon$4$$anonfun$1.apply(ConcurrentRestrictions.scala:160)
    at sbt.CompletionService$$anon$2.call(CompletionService.scala:30)
    at java.util.concurrent.FutureTask.run(FutureTask.java:262)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
    at java.util.concurrent.FutureTask.run(FutureTask.java:262)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:745)

我正在使用 Play 2.1.0

我猜我可以使用继承并传递一个实现 MyInterface 的类的具体实例,它是我想要呈现的所有类型的超类,但我真的很想如果可能,将接口传递给模板。

我是否试图做一些 Play (2.1.0) 不允许的事情?

【问题讨论】:

    标签: java scala templates playframework-2.0


    【解决方案1】:

    在重构我的原型以使用继承而不是多态的过程中,我偶然发现了这个问题。我在我的界面中定义了一个getId() 方法,该方法似乎与ebean ORM 产生了冲突。当我在接口和实现类中将其重命名为getAssetId() 时,项目将编译并且视图将呈现List&lt;MyInterface&gt; 就好了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-19
      • 1970-01-01
      • 2019-08-29
      • 2015-02-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多