Skip to content

Commit 290c61c

Browse files
committed
Set up initial Play 2.6.0 project
`sbt new playframework/play-java-seed.g8` See https://playframework.com/download#seeds
0 parents  commit 290c61c

File tree

21 files changed

+297
-0
lines changed

21 files changed

+297
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package controllers;
2+
3+
import play.data.Form;
4+
import play.data.FormFactory;
5+
import play.mvc.Controller;
6+
import play.mvc.Result;
7+
8+
import javax.inject.Inject;
9+
10+
// Add the following to conf/routes
11+
/*
12+
GET /$model;format="camel"$ controllers.$model;format="Camel"$Controller.$model;format="camel"$Get
13+
POST /$model;format="camel"$ controllers.$model;format="Camel"$Controller.$model;format="camel"$Post
14+
*/
15+
16+
/**
17+
* $model;format="Camel"$ form controller for Play Java
18+
*/
19+
public class $model;format="Camel"$Controller extends Controller {
20+
21+
private final Form<$model;format="Camel"$Data> $model;format="camel"$Form;
22+
23+
@Inject
24+
public $model;format="Camel"$Controller(FormFactory formFactory) {
25+
this.$model;format="camel"$Form = formFactory.form($model;format="Camel"$Data.class);
26+
}
27+
28+
public Result $model;format="camel"$Get() {
29+
return ok(views.html.$model;format="camel"$.form.render($model;format="camel"$Form));
30+
}
31+
32+
public Result $model;format="camel"$Post() {
33+
Form<$model;format="Camel"$Data> boundForm = $model;format="camel"$Form.bindFromRequest();
34+
if (boundForm.hasErrors()) {
35+
return badRequest(views.html.$model;format="camel"$.form.render(boundForm));
36+
} else {
37+
$model;format="Camel"$Data $model;format="camel"$ = boundForm.get();
38+
flash("success", "$model;format="Camel"$ " + $model;format="camel"$);
39+
return redirect(routes.$model;format="Camel"$Controller.$model;format="camel"$Get());
40+
}
41+
}
42+
43+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package controllers;
2+
3+
import play.data.validation.Constraints;
4+
5+
public class $model;format="Camel"$Data {
6+
7+
@Constraints.Required
8+
private String name;
9+
10+
@Constraints.Required
11+
private Integer age;
12+
13+
public $model;format="Camel"$Data() {
14+
}
15+
16+
public String getName() {
17+
return name;
18+
}
19+
20+
public void setName(String name) {
21+
this.name = name;
22+
}
23+
24+
public Integer getAge() {
25+
return age;
26+
}
27+
28+
public void setAge(Integer age) {
29+
this.age = age;
30+
}
31+
32+
@Override
33+
public String toString() {
34+
return String.format("$model;format="Camel"$Data(%s, %s)", name, age);
35+
}
36+
37+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
@($model;format="camel"$Form: Form[$model;format="Camel"$Data])
2+
3+
<h1>$model;format="camel"$ form</h1>
4+
5+
@flash.getOrDefault("success", "")
6+
7+
@helper.form(action = routes.$model;format="Camel"$Controller.$model;format="camel"$Post()) {
8+
@helper.CSRF.formField
9+
@helper.inputText($model;format="camel"$Form("name"))
10+
@helper.inputText($model;format="camel"$Form("age"))
11+
<input type="submit" value="submit"/>
12+
}

.g8/form/default.properties

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
description = Generates a Controller with form handling
2+
model = user

.g8/form/generated-test/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Temporary file until g8-scaffold will generate "test" directory
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package controllers;
2+
3+
import org.junit.Test;
4+
import play.Application;
5+
import play.filters.csrf.*;
6+
import play.inject.guice.GuiceApplicationBuilder;
7+
import play.mvc.*;
8+
import play.test.WithApplication;
9+
10+
import java.util.HashMap;
11+
12+
import static org.junit.Assert.assertEquals;
13+
import static play.mvc.Http.RequestBuilder;
14+
import static play.mvc.Http.Status.OK;
15+
import static play.test.Helpers.*;
16+
import static play.api.test.CSRFTokenHelper.*;
17+
18+
public class $model;format="Camel"$ControllerTest extends WithApplication {
19+
20+
@Override
21+
protected Application provideApplication() {
22+
return new GuiceApplicationBuilder().build();
23+
}
24+
25+
@Test
26+
public void test$model;format="Camel"$Get() {
27+
RequestBuilder request = new RequestBuilder()
28+
.method(GET)
29+
.uri("/$model;format="camel"$");
30+
31+
Result result = route(app, request);
32+
assertEquals(OK, result.status());
33+
}
34+
35+
@Test
36+
public void test$model;format="Camel"$Post() {
37+
HashMap<String, String> formData = new HashMap<>();
38+
formData.put("name", "play");
39+
formData.put("age", "4");
40+
RequestBuilder request = addCSRFToken(new RequestBuilder()
41+
.header(Http.HeaderNames.HOST, "localhost")
42+
.method(POST)
43+
.bodyForm(formData)
44+
.uri("/$model;format="camel"$"));
45+
46+
Result result = route(app, request);
47+
assertEquals(SEE_OTHER, result.status());
48+
}
49+
50+
}

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
target

app/controllers/HomeController.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package controllers;
2+
3+
import play.mvc.*;
4+
5+
/**
6+
* This controller contains an action to handle HTTP requests
7+
* to the application's home page.
8+
*/
9+
public class HomeController extends Controller {
10+
11+
/**
12+
* An action that renders an HTML page with a welcome message.
13+
* The configuration in the <code>routes</code> file means that
14+
* this method will be called when the application receives a
15+
* <code>GET</code> request with a path of <code>/</code>.
16+
*/
17+
public Result index() {
18+
return ok(views.html.index.render());
19+
}
20+
21+
}

app/views/index.scala.html

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
@()
2+
3+
@main("Welcome to Play") {
4+
<h1>Welcome to Play!</h1>
5+
}

app/views/main.scala.html

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
@*
2+
* This template is called from the `index` template. This template
3+
* handles the rendering of the page header and body tags. It takes
4+
* two arguments, a `String` for the title of the page and an `Html`
5+
* object to insert into the body of the page.
6+
*@
7+
@(title: String)(content: Html)
8+
9+
<!DOCTYPE html>
10+
<html lang="en">
11+
<head>
12+
@* Here's where we render the page title `String`. *@
13+
<title>@title</title>
14+
<link rel="stylesheet" media="screen" href="@routes.Assets.versioned("stylesheets/main.css")">
15+
<link rel="shortcut icon" type="image/png" href="@routes.Assets.versioned("images/favicon.png")">
16+
</head>
17+
<body>
18+
@* And here's where we render the `Html` object containing
19+
* the page content. *@
20+
@content
21+
22+
<script src="@routes.Assets.versioned("javascripts/main.js")" type="text/javascript"></script>
23+
</body>
24+
</html>

0 commit comments

Comments
 (0)