ace
ace

Reputation: 12024

Java controllers in Scala project in play framework?

Is it possible to have both java and scala controllers and views in play framework project when the project is created with scala option?

Upvotes: 6

Views: 2121

Answers (1)

Ludovico Fischer
Ludovico Fischer

Reputation: 1602

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.

Upvotes: 4

Related Questions