Play框架的模板引擎中是否存在“instanceof”功能?(Is there an “instanceof” functionality in the template engine of the

编程入门 行业动态 更新时间:2024-10-28 15:21:55
Play框架的模板引擎中是否存在“instanceof”功能?(Is there an “instanceof” functionality in the template engine of the Play framework?)

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

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

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

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

I would like to know if there is some kind of instanceof functionality in the template engine of the Play (2.0) framework. This would allow me to effectively use the inheritance structure in my model layer.

This is basically what I would like (except that this template doesn't compile):

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

Edit: I mean in specific Play for Java.

最满意答案

使用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> } } }

Use Scala pattern matching.

@(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> } } }

更多推荐

本文发布于:2023-08-02 14:44:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1375967.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:是否存在   框架   模板   功能   引擎

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!