Skip to content
Snippets Groups Projects
Commit 7f850a04 authored by Will Billingsley's avatar Will Billingsley
Browse files

Added example of a route with an optional id query parameter

parent 23768e92
Branches master
No related tags found
No related merge requests found
......@@ -7,6 +7,9 @@ import play.libs.Json;
import play.mvc.Controller;
import play.mvc.Result;
import java.util.Optional;
import java.util.Random;
/**
* This controller contains an action to handle HTTP requests
* to the application's home page.
......@@ -44,4 +47,30 @@ public class ExamplesController extends Controller {
}
private String chars = "abcdefghijklmnopqrstuvwxyz0123456789";
private Random random = new Random(System.currentTimeMillis());
/**
* Generates an 8 character random string
* @return
*/
private String randomId() {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 8; i++) {
int idx = random.nextInt(chars.length());
sb.append(chars.charAt(idx));
}
return sb.toString();
}
public Result game(String id) {
if (id != null) {
return ok("The id was " + id);
} else {
return redirect("/example/game?id=" + randomId());
}
}
}
......@@ -8,5 +8,7 @@ GET / controllers.HomeController.index
GET /example/json controllers.ExamplesController.returnJson()
POST /example/json controllers.ExamplesController.receiveJsonPost()
GET /example/game controllers.ExamplesController.game(id ?= null)
# Map static resources from the /public folder to the /assets URL path
GET /assets/*file controllers.Assets.versioned(path="/public", file: Asset)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment