6

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

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks. So it looks like scala file can be anywhere under app/controllers regardless of its package declaration. But Java files must follow directory structure according to the package.
Well I did not try java controller in scala project but I did try java class that is not a controller but like a service class but play hangs and does not work even if I just put the java file and not even being used.
That seems strange. Maybe it is better if you ask another question with a more detailed description of this problem (where exactly did you put that Java file?)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.