Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

init

  • Loading branch information...
commit 7908f7117ebe60152dbb899ead6aa34193255961 0 parents
@jamesward jamesward authored
16 .gitignore
@@ -0,0 +1,16 @@
+logs
+project/project
+project/target
+target
+tmp
+.history
+dist
+/.idea
+/*.iml
+/out
+/.idea_modules
+/.classpath
+/.project
+/RUNNING_PID
+/.settings
+activator-sbt-*-shim.sbt
13 LICENSE
@@ -0,0 +1,13 @@
+Copyright 2013 Typesafe, Inc.
+
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
4 activator.properties
@@ -0,0 +1,4 @@
+name=hello-play-scala
+title=Hello Play Framework (Scala Only)
+description=Play Framework is the High Velocity Web Framework for Java and Scala. Play is based on a lightweight, stateless, web-friendly architecture. Built on Akka, Play provides predictable and minimal resource comsumption (CPU, memory, threads) for highly-scalable applications. This app will teach you how to start building Play 2.1 apps with Scala.
+tags=Basics,play,scala,starter
9 app/controllers/Application.scala
@@ -0,0 +1,9 @@
+package controllers
+
+import play.api.mvc.{Action, Controller}
+
+object Application extends Controller {
+ def index = Action {
+ Ok(views.html.index.render("Hello Play Framework"))
+ }
+}
9 app/views/index.scala.html
@@ -0,0 +1,9 @@
+@(message: String)
+
+@main("Welcome to Play Framework") {
+
+ <div class="well">
+ <h1>@message</h1>
+ </div>
+
+}
28 app/views/main.scala.html
@@ -0,0 +1,28 @@
+@(title: String)(content: Html)
+
+<!DOCTYPE html>
+<html>
+<head>
+ <title>@title</title>
+ <link rel='shortcut icon' type='image/png' href='@routes.Assets.at("images/favicon.png")'>
+ <link rel='stylesheet' href='@routes.WebJarAssets.at(WebJarAssets.locate("bootstrap.min.css"))'>
+ <script type='text/javascript' src='@routes.WebJarAssets.at(WebJarAssets.locate("jquery.min.js"))'></script>
+ <style>
+ body {
+ margin-top: 50px;
+ }
+ </style>
+</head>
+<body>
+ <div class="navbar navbar-fixed-top">
+ <div class="navbar-inner">
+ <div class="container-fluid">
+ <a id="titleLink" class="brand" href="/">@title</a>
+ </div>
+ </div>
+ </div>
+ <div class="container">
+ @content
+ </div>
+</body>
+</html>
12 build.sbt
@@ -0,0 +1,12 @@
+import play.Project._
+
+name := "hello-play-scala"
+
+version := "1.0-SNAPSHOT"
+
+libraryDependencies ++= Seq(
+ "org.webjars" %% "webjars-play" % "2.2.0",
+ "org.webjars" % "bootstrap" % "2.3.1"
+)
+
+playScalaSettings
68 conf/application.conf
@@ -0,0 +1,68 @@
+# This is the main configuration file for the application.
+# ~~~~~
+
+# Secret key
+# ~~~~~
+# The secret key is used to secure cryptographics functions.
+# If you deploy your application to several instances be sure to use the same key!
+application.secret="b]4y[mp`niu[i><trTJgfF2McPDXQ4_Nm;6Z?q=89=7vvvZbOn3`wcbX/E::81?T"
+
+# The application languages
+# ~~~~~
+application.langs="en"
+
+# Global object class
+# ~~~~~
+# Define the Global object class for this application.
+# Default to Global in the root package.
+# application.global=Global
+
+# Router
+# ~~~~~
+# Define the Router object to use for this application.
+# This router will be looked up first when the application is starting up,
+# so make sure this is the entry point.
+# Furthermore, it's assumed your route file is named properly.
+# So for an application router like `conf/my.application.Router`,
+# you may need to define a router file `my.application.routes`.
+# Default to Routes in the root package (and `conf/routes`)
+# application.router=my.application.Routes
+
+# Database configuration
+# ~~~~~
+# You can declare as many datasources as you want.
+# By convention, the default datasource is named `default`
+#
+# db.default.driver=org.h2.Driver
+# db.default.url="jdbc:h2:mem:play"
+# db.default.user=sa
+# db.default.password=""
+#
+# You can expose this datasource via JNDI if needed (Useful for JPA)
+# db.default.jndiName=DefaultDS
+
+# Evolutions
+# ~~~~~
+# You can disable evolutions if needed
+# evolutionplugin=disabled
+
+# Ebean configuration
+# ~~~~~
+# You can declare as many Ebean servers as you want.
+# By convention, the default server is named `default`
+#
+# ebean.default="models.*"
+
+# Logger
+# ~~~~~
+# You can also configure logback (http://logback.qos.ch/), by providing a logger.xml file in the conf directory .
+
+# Root logger:
+logger.root=ERROR
+
+# Logger used by the framework:
+logger.play=INFO
+
+# Logger provided to your application:
+logger.application=DEBUG
+
9 conf/routes
@@ -0,0 +1,9 @@
+# Routes
+# This file defines all application routes (Higher priority routes first)
+# ~~~~
+
+GET / controllers.Application.index()
+
+# Map static resources from the /public folder to the /assets URL path
+GET /assets/*file controllers.Assets.at(path="/public", file)
+GET /webjars/*file controllers.WebJarAssets.at(file)
1  project/build.properties
@@ -0,0 +1 @@
+sbt.version=0.13.0
8 project/plugins.sbt
@@ -0,0 +1,8 @@
+// Comment to get more information during initialization
+logLevel := Level.Warn
+
+// The Typesafe repository
+resolvers += "Typesafe repository" at "http://repo.typesafe.com/typesafe/releases/"
+
+// Use the Play sbt plugin for Play projects
+addSbtPlugin("com.typesafe.play" % "sbt-plugin" % "2.2.0")
BIN  public/images/favicon.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
30 test/ApplicationSpec.scala
@@ -0,0 +1,30 @@
+import org.specs2.mutable._
+import org.specs2.runner._
+import org.junit.runner._
+
+import play.api.test._
+import play.api.test.Helpers._
+
+/**
+ * Add your spec here.
+ * You can mock out a whole application including requests, plugins etc.
+ * For more information, consult the wiki.
+ */
+@RunWith(classOf[JUnitRunner])
+class ApplicationSpec extends Specification {
+
+ "Application" should {
+
+ "send 404 on a bad request" in new WithApplication{
+ route(FakeRequest(GET, "/boum")) must beNone
+ }
+
+ "render the index page" in new WithApplication{
+ val home = route(FakeRequest(GET, "/")).get
+
+ status(home) must equalTo(OK)
+ contentType(home) must beSome.which(_ == "text/html")
+ contentAsString(home) must contain ("Your new application is ready.")
+ }
+ }
+}
24 test/IntegrationSpec.scala
@@ -0,0 +1,24 @@
+import org.specs2.mutable._
+import org.specs2.runner._
+import org.junit.runner._
+
+import play.api.test._
+import play.api.test.Helpers._
+
+/**
+ * add your integration spec here.
+ * An integration test will fire up a whole play application in a real (or headless) browser
+ */
+@RunWith(classOf[JUnitRunner])
+class IntegrationSpec extends Specification {
+
+ "Application" should {
+
+ "work from within a browser" in new WithBrowser {
+
+ browser.goTo("http://localhost:" + port)
+
+ browser.pageSource must contain("Your new application is ready.")
+ }
+ }
+}
99 tutorial/index.html
@@ -0,0 +1,99 @@
+<html>
+<head>
+ <title>Hello Play Framework - Activator Template</title>
+</head>
+<body>
+<div>
+ <p>You've just created a simple Play Framework application! Now lets explore the code and make some changes.</p>
+
+ <h2> View the App</h2>
+
+ <p>
+ Once the application has been compiled and the server started, your application can be accessed at: <a href="http://localhost:9000" target="_blank">http://localhost:9000</a><br/>
+ Check in <a href="#run" class="shortcut">Run</a> to see the server status.<br/>
+ When you make an HTTP request to that URL, the Play server figures out what code to execute which will handle the request and return a response. In this application the request handler for requests to the root URL (e.g. "/") are handled by a Java Controller. You can use Java and Scala to create your controllers.<br/>
+ Controllers asynchronously return HTTP responses of any content type (i.e. HTML, JSON, binary).
+ </p>
+</div>
+<div>
+ <h2>UNDER CONSTRUCTION</h2>
+</div>
+<!--
+<div>
+ <h2> Code Overview</h2>
+
+ <p>A Play application has very few required files. This example tries to illustrate a number of key concepts in Play so it has a handful of files to explain. The <a href="#code/app" class="shortcut">app</a> directory contains the application's source code, including Java, Scala, server-side templates, and server-side compiled assets for the browser (e.g. JavaScript, CoffeeScript, LESS). The <a href="#code/conf" class="shortcut">conf</a> directory contains the <a href="#code/conf/application.conf" class="shortcut">application.conf</a> config file and the <a href="#code/conf/routes" class="shortcut">routes</a> file</a>. The <a href="#code/project" class="shortcut">project</a> directory contains the SBT build information for the app. The default place for static assets (e.g. images, HTML, CSS, JavaScript) is the <a href="#code/public" class="shortcut">public</a> directory. Unit, functional, and integration tests (Java and Scala) go in the <a href="#code/test" class="shortcut">test</a> directory.</p>
+</div>
+<div>
+ <h2> HTTP Routing</h2>
+
+ <p>
+ The <a href="#code/conf/routes" class="shortcut">routes</a> file contains mappings between HTTP request verbs &amp; paths and the code that controller method that handles the requests. Route paths can be parametrized to support RESTful style URLs. The following mapping will handle HTTP GET requests to "/" using the <strong>controllers.Application.index</strong> method:
+ <pre><code>GET / controllers.Application.index</code></pre>
+ You can try out the routing by first making a request to an unmatched route, like: <a href="http://localhost:9000/foo" target="_blank">http://localhost:9000/foo</a><br/>
+ This should produce an error because there isn't a route that matches the request.
+ Then edit the <a href="#code/conf/routes" class="shortcut">routes</a> to include a new route:
+ <pre><code>GET /foo controllers.Application.index</code></pre>
+ Now retry the request and you should see the default index page.<br/>
+ By default the routes are compiled by Play so that they can be used in a type-safe way. This prevents issues caused by incorrect URL usages.
+ </p>
+</div>
+<div>
+ <h2> Controllers</h2>
+
+ <p>
+ HTTP GET requests to <a href="http://localhost:9000/" target="_blank">http://localhost:9000/</a> are handled by the index method defined in the <a href="#code/app/controllers/Application.java" class="shortcut">Application.java</a> file. This static method returns a <strong>Result</strong> which in this case consists of a server-side rendered template and an HTTP 200 response which is represented by the <strong>ok</strong> method call. You can also use non-static methods along with Dependency Injection frameworks like Spring, Guice, and CDI.<br/>
+ You can change the "Hello Play Framework" string in <a href="#code/app/controllers/Application.java" class="shortcut">Application.java</a> a different value, like "Hello from Play", then save the file and reload <a href="http://localhost:9000/" target="_blank">the index page</a> in your browser. Play will automatically recompile the controller and you should immediately see your changes.<br/>
+ If your changes result in a compile error you will see the error in your browser (after a refresh) and in the <a href="#compile" class="shortcut">Compile Plugin</a>. Try it by removing one of the double-quotes in the <strong>index</strong> method's string, save the file, and reload the index page. Fix the error before you continue.<br/>
+ </p>
+</div>
+<div>
+ <h2> Templates</h2>
+
+ <p>
+ The <strong>Application.index</strong> method uses a template which is compiled by Play into a function. The source is in the <a href="#code/app/views/index.scala.html" class="shortcut">index.scala.html</a> file and is available by calling the <strong>views.html.index.render</strong> function. This template takes a parameter of a <strong>String</strong> so the compiled function also takes a <strong>String</strong> parameter. The default templating language in Play is Scala, but a number of other templating languages (like Java, Groovy, and Dust) can also be plugged in.<br/>
+ The <a href="#code/app/views/index.scala.html" class="shortcut">index template</a> uses the <a href="#code/app/views/main.scala.html" class="shortcut">main template</a> by calling the <strong>main</strong> function with the required parameters.<br/>
+ Notice that the templates do not use hard-coded URLs and instead use Play's reverse routing, like:
+ <pre><code>@routes.Assets.at("images/favicon.png")</code></pre>
+ This provides a type-safe way to specify URLs in an application. If the URL to a file changes, then automatically, the new value will be used throughout all of the URL references. You can use the reverse router anywhere in your code, like redirects in controllers.<br/>
+ Like controllers (and all source code in Play) you can make changes to templates which triggers a recompile. Then simply refresh the page to see the changes. Try it out by making a change to the <a href="#code/app/views/index.scala.html" class="shortcut">index template</a>, saving the file, and then refreshing the <a href="http://localhost:9000/" target="_blank">index page</a> in your browser.
+ </p>
+</div>
+<div>
+ <h2> Static Assets</h2>
+
+ <p>
+ This application has a route to handle loading static assets from the <a href="#code/public" class="shortcut">public</a> directory:
+ <pre><code>GET /assets/*file controllers.Assets.at(path="/public", file)</code></pre>
+ A request to <a href="http://localhost:9000/assets/images/favicon.png" target="_blank">http://localhost:9000/assets/images/favicon.png</a> will use this route to find the <a href="#code/public/images/favicon.png" class="shortcut">favicon.png</a> file in the <strong>public/images</strong> directory.<br/>
+ You can put any static assets into the public directory and have them accessible in this way.
+ </p>
+</div>
+<div>
+ <h2> Test the App</h2>
+
+ <p>
+ This application includes a number of tests which are automatically run after each successful compile. You can see the test results in the <a href="#test" class="shortcut">Test plugin</a>. Due to changes that you may have made to your application source code, some of the tests might now be failing. The source for the tests is located in the <a href="#code/test" class="shortcut">test</a> directory. The <a href="#code/test/ApplicationTest.java" class="shortcut">test/ApplicationTest.java</a> Java class contains a number of unit and functional tests for the <strong>Application</strong>. The <a href="#code/test/IntegrationTest.java" class="shortcut">IntegrationTest.java</a> file contains an test which uses Solenium to test the actual UI in a browser. The default testing framework for Java in Play is JUnit, but you can plug any testing framework in.<br/>
+ If you modify a test and save the changes, then the tests will automatically be re-run.
+ </p>
+</div>
+<div>
+ <h2> SBT Build</h2>
+
+ <p>
+ SBT is the build system underneath Play applications. It is responsible for resolving dependencies, compiling the project, running the tests, etc. The primary build definition file is the <a href="#code/project/Build.scala" class="shortcut">Build.scala</a> file. In that file you will see a comma-seperated list of dependencies which can be modified to suit your needs. Play includes a number of libraries which can selectively be turned on/off. For instance if you want to use JPA with Play, uncomment the <strong>javaJpa</strong> dependency. You can also add your own dependencies in the SBT form:
+ <pre><code>"group" % "artifact" % "version"</code></pre>
+ </p>
+</div>
+-->
+<div>
+ <h2> Further Learning</h2>
+
+ <p>
+ The <a href="http://www.playframework.com/documentation" target="_blank">Play Documentation</a> contains much more exhaustive details and also covers a number of other topics which haven't been addressed in this tutorial.<br/>
+ <a href="http://stackoverflow.com" target="_blank">StackOverflow</a> is a great place ask questions about Play.<br/>
+ <a href="http://groups.google.com/group/play-framework" target="_blank">The play-framework</a> Google Group is a great place to discuss Play.<br/>
+ </p>
+</div>
+</body>
+</html>
Please sign in to comment.
Something went wrong with that request. Please try again.