【问题标题】:Is there an "instanceof" functionality in the template engine of the Play framework?Play 框架的模板引擎中是否有“instanceof”功能?
【发布时间】:2012-03-25 19:08:06
【问题描述】:

我想知道 Play (2.0) 框架的模板引擎中是否有某种instanceof 功能。这将使我能够有效地使用模型层中的继承结构。

这基本上是我想要的(除了这个模板不编译):

@(instance: Superclass)

@main { 
    @if(instance instanceof Subclass) {
        Subclass instanceOfSubclass = (Subclass) instance;    
    }
}

编辑:我的意思是在特定的 Play for Java 中。

【问题讨论】:

    标签: java inheritance playframework-2.0 instanceof


    【解决方案1】:

    使用 Scala 模式匹配。

    @(instance: Superclass)
    
    @main { 
      @instance match {
        case foo: Foo => {
          <a href="@routes.Foo.index()">@foo.title</a>
        }
        case bar: Bar => {
          <a href="@routes.Bar.index()">@bar.title</a>
        }
      }
    }
    

    【讨论】:

    • 它在 java 世界中工作对我来说很奇怪,因为 FooBar 必须具有用于模式匹配的提取器(未应用)。我现在很想知道这可以解决问题。有什么线索吗?
    • 我不确定。一方面,这种模式匹配只检查类型。另一方面,Play 仍然在 Java 中使用了一些字节码魔法。否则,Scala 模板在为 Java 和 Scala 提供 API 的框架中没有多大意义。
    • 确实...我一定很累... listi.G 绝对不需要敌人
    猜你喜欢
    • 1970-01-01
    • 2018-10-25
    • 2023-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-21
    相关资源
    最近更新 更多