diff --git a/src/docs/asciidoc/34-compiler-options.adoc b/src/docs/asciidoc/34-compiler-options.adoc index b700ec93..23665717 100644 --- a/src/docs/asciidoc/34-compiler-options.adoc +++ b/src/docs/asciidoc/34-compiler-options.adoc @@ -6,4 +6,13 @@ If your Play application requires additional Scala compiler flags, you can add t .build.gradle ---- include::{samplesCodeDir}/configure-compiler/groovy/build.gradle[tag=additional-params] +---- + +When working with Twirl templates in Java-based project, the Java default imports needs to be enabled to include necessary +conversions of Scala objects to Java objects, e.g. Scala forms to Java forms. + +[source,groovy] +.build.gradle +---- +include::{samplesCodeDir}/basic-java/groovy/build.gradle[tag=enable-java-default-imports-in-templates] ---- \ No newline at end of file diff --git a/src/docs/samples/basic-java/groovy/app/controllers/Application.java b/src/docs/samples/basic-java/groovy/app/controllers/Application.java new file mode 100644 index 00000000..2c687082 --- /dev/null +++ b/src/docs/samples/basic-java/groovy/app/controllers/Application.java @@ -0,0 +1,31 @@ +package controllers; + +import play.mvc.Controller; +import play.mvc.Result; +import org.apache.commons.lang3.StringUtils; +import play.data.Form; +import play.data.FormFactory; +import javax.inject.*; +import model.*; + +@Singleton +public class Application extends Controller { + + private FormFactory ff; + + @Inject + public Application(FormFactory ff) { + this.ff = ff; + } + + public Result index() { + + Form userForm = ff.form(User.class).bindFromRequest(); + + return ok(views.html.indexUserForm.render( + StringUtils.trim(" Your new application is ready. "), + userForm + )); + } + +} diff --git a/src/docs/samples/basic-java/groovy/app/model/User.java b/src/docs/samples/basic-java/groovy/app/model/User.java new file mode 100644 index 00000000..91ea35db --- /dev/null +++ b/src/docs/samples/basic-java/groovy/app/model/User.java @@ -0,0 +1,8 @@ +package model; + +public class User { + + public String username; + public String mobile; + +} \ No newline at end of file diff --git a/src/docs/samples/basic-java/groovy/app/views/indexUserForm.scala.html b/src/docs/samples/basic-java/groovy/app/views/indexUserForm.scala.html new file mode 100644 index 00000000..168cbdc3 --- /dev/null +++ b/src/docs/samples/basic-java/groovy/app/views/indexUserForm.scala.html @@ -0,0 +1,27 @@ +@(message: String, userForm : play.data.Form[model.User]) + +@main("Welcome to Play") { + +

@message

+ +

User Form

+ +@helper.form(action = routes.Application.index, 'id -> "userFormId") { + +
+ @helper.inputText( + field = userForm("username"), + args = '_label -> "Username", 'id -> "username" + ) +
+ +
+ @helper.inputText( + field = userForm("mobile"), + args = '_label -> "Mobile", 'id -> "mobile" + ) +
+ +} + +} diff --git a/src/docs/samples/basic-java/groovy/app/views/main.scala.html b/src/docs/samples/basic-java/groovy/app/views/main.scala.html new file mode 100644 index 00000000..5025aa5b --- /dev/null +++ b/src/docs/samples/basic-java/groovy/app/views/main.scala.html @@ -0,0 +1,15 @@ +@(title: String)(content: Html) + + + + + + @title + + + + + + @content + + diff --git a/src/docs/samples/basic-java/groovy/basic-java.sample.conf b/src/docs/samples/basic-java/groovy/basic-java.sample.conf new file mode 100644 index 00000000..3aaf111d --- /dev/null +++ b/src/docs/samples/basic-java/groovy/basic-java.sample.conf @@ -0,0 +1,2 @@ +executable: gradle +args: tasks diff --git a/src/docs/samples/basic-java/groovy/build.gradle b/src/docs/samples/basic-java/groovy/build.gradle new file mode 100644 index 00000000..68d962eb --- /dev/null +++ b/src/docs/samples/basic-java/groovy/build.gradle @@ -0,0 +1,48 @@ +// tag::use-plugin[] +plugins { + id 'org.gradle.playframework' version '0.9' +} + +repositories { + jcenter() + maven { + name "lightbend-maven-release" + url "https://repo.lightbend.com/lightbend/maven-releases" + } + ivy { + name "lightbend-ivy-release" + url "https://repo.lightbend.com/lightbend/ivy-releases" + layout "ivy" + } +} +// end::use-plugin[] + +play { + injectedRoutesGenerator = true +} + +dependencies { + implementation 'commons-lang:commons-lang:2.6' + testImplementation "com.google.guava:guava:17.0" + testImplementation "org.scalatestplus.play:scalatestplus-play_2.12:3.1.2" + implementation "com.typesafe.play:play-guice_2.12:2.6.15" + implementation "ch.qos.logback:logback-classic:1.2.3" + + implementation group: 'com.typesafe.play', name: 'play-java-forms_2.12', version: '2.6.15' +} + +// tag::enable-java-default-imports-in-templates[] +sourceSets { + main { + twirl { + defaultImports = org.gradle.playframework.sourcesets.TwirlImports.JAVA + } + } +} + +// alternatively +// compilePlayTwirlTemplates { +// defaultImports = org.gradle.playframework.sourcesets.TwirlImports.JAVA +// } + +// end::enable-java-default-imports-in-templates[] diff --git a/src/docs/samples/basic-java/groovy/conf/application.conf b/src/docs/samples/basic-java/groovy/conf/application.conf new file mode 100644 index 00000000..44d32000 --- /dev/null +++ b/src/docs/samples/basic-java/groovy/conf/application.conf @@ -0,0 +1,44 @@ +# This is the main configuration file for the application. +# ~~~~~ + +# Secret key +# ~~~~~ +# The secret key is used to secure cryptographics functions. +# +# This must be changed for production, but we recommend not changing it in this file. +# +# See http://www.playframework.com/documentation/latest/ApplicationSecret for more details. +play.http.secret.key="dxbAjiDdqlIV83LY<:;hSxql?tG`CPNgXEXt2asjk>lYQ""") + contentAsString(home) must include ("""""") + contentAsString(home) must include ("""""") + } + + "tests can use commons-lang play dependency" in { + StringUtils.reverse("foobar") mustBe "raboof" + } + + "tests can use guava play-test dependency" in { + Lists.newArrayList("foo", "bar").size() mustBe 2 + } + } +} diff --git a/src/docs/samples/basic-java/groovy/test/IntegrationSpec.scala b/src/docs/samples/basic-java/groovy/test/IntegrationSpec.scala new file mode 100644 index 00000000..c09b83f1 --- /dev/null +++ b/src/docs/samples/basic-java/groovy/test/IntegrationSpec.scala @@ -0,0 +1,22 @@ +import org.junit.runner.RunWith +import org.scalatest.junit.JUnitRunner +import org.scalatestplus.play._ +import org.scalatestplus.play.guice.GuiceOneServerPerTest + +@RunWith(classOf[JUnitRunner]) +class IntegrationSpec extends PlaySpec + with OneBrowserPerTest + with GuiceOneServerPerTest + with HtmlUnitFactory + with ServerProvider { + + "Application" should { + + "work from within a browser" in { + + go to ("http://localhost:" + port) + + pageSource must include ("Your new application is ready.") + } + } +}