Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Documentation for 'data binding'

  • Loading branch information...
commit 8dca0377aa3908299cf836ef7e88b6a6fa00d392 1 parent 96371cb
@guillaumebort guillaumebort authored
View
13 documentation/manual/controllers.textile
@@ -60,9 +60,9 @@ bc. def index = "Commons/home.html".asTemplate
In this case the **app/views/Commons/home.html** template will be rendered.
-You can also pass values to be included during the template evaluation. Values are passed in form of @(String,Any)@ tuples, where the first member will be used as variable name in the template, and the second member as variable value.
+You can also pass values to be included during the template evaluation. Values are passed in form of @(Symbol,Any)@ tuples, where the first member will be used as variable name in the template, and the second member as variable value.
-bc. def index = Template("now" -> new Date(), "numbers" -> List(1,2,3))
+bc. def index = Template('now -> new Date(), 'numbers -> List(1,2,3))
p(note). In a Java controller you never specify the template variable name, and Play automatically determine it by extracting the Java variable local name.
@@ -254,7 +254,7 @@ bc. @Before def logRequests {
println("New request…")
}
-You see that here, the @beforeEachAction@ method does not return any value. So the request execution will continue by invoking the next interceptors and eventually the action method.
+You see that here, the @logRequests@ method does not return any value. So the request execution will continue by invoking the next interceptors and eventually the action method.
But you can also write some interceptor that return a value:
@@ -281,6 +281,7 @@ Scala Traits can be used to compose controller more effeciently by mixing severa
For example the following **Secure** trait add a seucrity interceptor to any controller applying the Trait:
bc. trait Secure {
+ self:Controller =>
@Before checkSecurity = {
session("username") match {
@@ -294,6 +295,8 @@ bc. trait Secure {
}
+p(note). Note that here we use the @self:Controller =>@ notation to indicate that this Trait can only be mixed with a @Controller@ type.
+
And you can use it to create a secured controller:
bc. object Application extends Controller with Secure {
@@ -301,3 +304,7 @@ bc. object Application extends Controller with Secure {
def index = <h1>Hello {connectedUser.name}!</h1>
}
+
+<hr>
+
+There is also small differences about %(next)"Data binding":dataBinding%
View
42 documentation/manual/dataBinding.textile
@@ -0,0 +1,42 @@
+h1. HTTP to Scala data binding
+
+Like in Java, you can retrieve HTTP parameters directly from the action method signature. The method parameter’s name must be the same as the HTTP parameter’s. This section explain the difference related to specific Scala types.
+
+h2. Binding Option[T] types
+
+Sometimes you are not sure that a specific parameter will be present in the HTTP request. In this case, you want probably bind this value to an @Option@ type:
+
+bc. def hello(name: Option[String]) = {
+ name.map("Hello " + _ + "!").getOrElse("Please give us your name!")
+}
+
+h2. Using Scala default parameter values
+
+Another way to handle the case where an HTTP parameter is missing, is to specify a default value for the method parameter.
+
+bc. def hello(name: String = "Guest") = {
+ "Hello " + name + "!"
+}
+
+An interesting side effect of this implementation is that the default parameter values are used as well for reverse routing. So for example you can ask Play to redirect to the @Hello@ action without specifying the **name** parameter:
+
+bc. def redirectToHello = Action(hello())
+
+h2. Binding case classes
+
+You can automatically ask Play to fill a more complex data structure, by specifying a **case class** as method parameter. For example:
+
+bc. case class User(name: String, email: String)
+
+And then, defining an action method:
+
+bc. def hello(user: User) = user match {
+ case User("guillaume", _) => "Howdy, guillaume!"
+ case User(name, _) => "Hello " + name
+}
+
+The same convention apply than for binding JavaBean in Java, ie:
+
+bc. /hello?user.name=Guillaume&user.email=gbo@zenexity.com
+
+p(note). Note that Play will automatically generate a default constructor for your case class if you don't define it yourself. However this constructor will be completely empty, and your cass class body will not been executed at object instantiation.
View
4 documentation/manual/hello-world.textile
@@ -169,13 +169,13 @@ object Application extends Controller {
def index = Template
- def sayHello(myName:String) = Template("myName" -> myName)
+ def sayHello(myName:String) = Template('myName -> myName)
}
We have declared the **myName** parameter in the action method signature, so it will automatically be filled with the value of the HTTP **myName** parameter, coming from the form submission.
-This new action also returns a @Template@ value, but this time it specify a value to be included in the template. You can specify as many values you want to send to the template, and for each you need to specify the template variable name. So each value is specified as a @(String, Any)@ tuple, where the first String value will be used a template variable name.
+This new action also returns a @Template@ value, but this time it specify a value to be included in the template. You can specify as many values you want to send to the template, and for each you need to specify the template variable name. So each value is specified as a @(Symbol, Any)@ tuple, where the first Symbol value will be used a template variable name.
!images/guide1-page!
View
2  documentation/manual/home.textile
@@ -19,7 +19,7 @@ h2. ‘Play Scala’ for Play Java developers
The Scala version of Play framework share the same philosophy and concepts than the Java version. Here are listed the most important changes.
# "Writing Play controllers in Scala":controllers
-# "HTTP to Scala data binding":#
+# "HTTP to Scala data binding":dataBinding
# "Dealing with Scala types in Play templates":#
# "Database access options":#
# "Testing your application":#
View
2  documentation/manual/install.textile
@@ -10,7 +10,7 @@ The included Scala version is **2.8.1**.
h2. Installing the Scala module locally
-The simpler way to work with Play and Scala is to download and install the Scala module locally. You can use the @play scala@ command to do that:
+The simpler way to work with Play and Scala is to download and install the Scala module locally. You can use the @play install scala@ command to do that:
bc. $ play install scala
~ _ _
Please sign in to comment.
Something went wrong with that request. Please try again.