Play 框架中的 Scala 项目中的 Java 控制器?
当使用 scala 选项创建项目时,是否可以在 play 框架项目中同时拥有 java 和 scala 控制器和视图?
Is it possible to have both java and scala controllers and views in play framework project when the project is created with scala option?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,可以在 Play 应用程序中混合使用 Java 和 Scala。您可以混合使用 Java 和 Scala 控制器以及 Groovy 和 Scala 模板。以下说明已经过 Play 测试! 1.2.2RC1。
我首先将 Java 和 Scala 控制器保存在
app/controllers
中。当然,Java 仅限于每个文件一个类,但在 Scala 中,包不与文件和目录绑定,因此您可以为 Scala 文件指定任何名称;只要您正确声明包,Play 就应该可以轻松找到它们。
Scala 模板应以
.scala.html
结尾,Groovy 模板应以纯.html
结尾。在 Scala 控制器中,您可以使用html.templatename
渲染 Scala 模板,使用Template("templatename") 渲染 Groovy 模板。
我从未尝试过从 Java 控制器渲染 Scala 模板,但我不明白为什么它不可行。
最后,这是一个关于 Scala/Java 互操作的有用提醒 。
Yes, it's possible to mix Java and Scala in a Play application. You can mix both Java and Scala controllers and Groovy and Scala templates. The instructions below have been tested with Play! 1.2.2RC1.
I would start by keeping both your Java and Scala controllers in
app/controllers
. Java is of course limited to one class per file, but in Scala packages are not tied to files and directories, so you can give your Scala file any name; as long as you declare the package correctly asPlay should have not trouble finding them.
Scala templates should end in
.scala.html
and Groovy templates in plain.html
. In a Scala controller, you render Scala templates withhtml.templatename
and Groovy templates withTemplate("templatename").
I have never tried rendering a Scala template from a Java controller, but I don't see why it should not be feasible.
Finally, here's a useful reminder on Scala/Java interop.