- The first time, it takes a very long time to fetch all dependencies—do what's on this slide before the lab.
- Download SBT: https://www.scala-sbt.org/download.html On Ubuntu, follow the “Linux (deb)” instructions. I have no experience doing this on anything other than Linux, but this may help: https://www.scala-sbt.org/1.x/docs/Setup.html
- Run:
cd cs151
sbt new playframework/play-java-seed.g8 --name=lab13
- Run
cd lab13
sbt run
- Point your browser to
localhost:9000
. You should see a welcome screen.
- We want to build a clicker app where students can respond to a clicker question. We won't build a web UI on the server—that's so 1990s. Instead, we'll send commands to the server such as this one:
http://localhost:9000/select/day13q1/C
To do that, you need to
- Make an entry in the
conf/routes
file
- Provide a handler method in a controller (in the
app/controller
folder)
Add this method to the HomeController
class:
public Result select(String problem, String choice) {
return ok("You selected " + choice + " for " + problem + "\n").as("text/plain");
}
- Save the file. Point your browser to
http://localhost:9000/select/day13q1/C
. What happens?
- Look at the section “The routes file syntax” in this page. Then fix up
conf/routes
so that your method gets called. What did you do?
- What output do you get now for
http://localhost:9000/select/day13q1/C
- Add this field to
HomeController
:
private static HashMap<String, HashMap<String, Integer>> results = new HashMap<>();
In the select
method, add this code before the return
statement:
if (results.get(problem) == null)
results.put(problem, new HashMap<>());
if (results.get(problem).get(choice) == null)
results.get(problem).put(choice, 0);
results.get(problem).put(choice,
results.get(problem).get(choice) + 1);
What does the code do?
- Add another method
public Result view(String problem)
that yields the map of counts for the given problem. Just turn the map results.get(problem)
into a string. What is your method?
- Add a route to it. Point your browser to
http://localhost:9000/select/day13q1/C
http://localhost:9000/select/day13q1/A
http://localhost:9000/select/day13q1/B
http://localhost:9000/select/day13q1/C
http://localhost:9000/view/day13q1
What happens?