Play 框架中的 Scala 项目中的 Java 控制器?

发布于 2024-11-16 06:34:47 字数 66 浏览 3 评论 0原文

当使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

财迷小姐 2024-11-23 06:34:47

是的,可以在 Play 应用程序中混合使用 Java 和 Scala。您可以混合使用 Java 和 Scala 控制器以及 Groovy 和 Scala 模板。以下说明已经过 Play 测试! 1.2.2RC1。

我首先将 Java 和 Scala 控制器保存在 app/controllers 中。当然,Java 仅限于每个文件一个类,但在 Scala 中,包不与文件和目录绑定,因此您可以为 Scala 文件指定任何名称;只要您正确声明包,

package controllers

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 as

package controllers

Play 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 with html.templatename and Groovy templates with Template("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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文