Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 949
ace Avatar asked Jun 17 '11 06:06

ace


1 Answers

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.

like image 192
Ludovico Fischer Avatar answered Oct 04 '22 00:10

Ludovico Fischer