Created
February 18, 2012 14:21
-
-
Save erikrozendaal/1859530 to your computer and use it in GitHub Desktop.
Play! 2.0
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# --- !Ups | |
create table users ( | |
name text primary key, | |
age int not null); | |
# --- !Downs | |
drop table users; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def page(name: String, format: String) = Action { | |
Ok(<p>You requested page {name} in format {format}</p>.toString).as(HTML) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import play.api.data._, Forms._, validation.Constraints._ | |
// Domain object | |
case class User(name: String, age: Int) | |
// Form definition | |
val userForm = Form( | |
mapping( | |
"name" -> nonEmptyText, | |
"age" -> number(min = 0, max = 150) | |
)(User.apply)(User.unapply)) | |
// Form submit action | |
def register = Action { implicit request => | |
userForm.bindFromRequest.fold( | |
errors => BadRequest(views.html.index(errors)), | |
user => Redirect(routes.Application.index)) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@(registrationForm: Form[User]) | |
@helper.form(action = routes.Application.register) { | |
@helper.inputText(registrationForm("name")) | |
@helper.inputText(registrationForm("age")) | |
<input type="submit" value="Register" /> | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@(message: String) | |
@main("Welcome to Play 2.0") { | |
<a href="@routes.Application.page("1984", "PDF")">page example</a> | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import play.api.db._, anorm._, play.api.Play.current | |
def registeredUsers = DB.withConnection { implicit connection => | |
SQL("select name, age from users")().map(row => User(row[String]("name"), row[Int]("age"))).toList | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment