8000 Update HelloSpark.java to add ArrayList and POST · taywils/java_spark_tutorial@73b418a · GitHub
[go: up one dir, main page]

Skip to content

Commit 73b418a

Browse files
committed
Update HelloSpark.java to add ArrayList and POST
1 parent 03c7206 commit 73b418a

File tree

1 file changed

+22
-11
lines changed

1 file changed

+22
-11
lines changed

src/main/java/HelloSpark.java

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,38 @@
11
import static spark.Spark.*;
22
import spark.*;
33

4+
import java.util.ArrayList;
5+
46
public class HelloSpark {
7+
// Just store POST data within a ArrayList for now
8+
public static ArrayList<String> things = new ArrayList<String>();
9+
510
public static void main(String[] args) {
6-
get(new Route("/hello") {
11+
get(new Route("/list") {
712
@Override
813
public Object handle(Request request, Response response) {
9-
return "Hello Spark MVC Framework!";
10-
}
11-
});
14+
StringBuilder html = new StringBuilder();
1215

13-
get(new Route("/goodbye") {
14-
@Override
15-
public Object handle(Request request, Response response) {
16-
return "Goodbye Spark MVC Framework!";
16+
if (HelloSpark.things.isEmpty()) {
17+
html.append("<b>Try adding some things to your list</b>");
18+
} else {
19+
html.append("<ul>");
20+
for (String thing : HelloSpark.things) {
21+
html.append("<li>").append(thing).append("</p>");
22+
}
23+
html.append("</ul>");
24+
}
25+
26+
return html.toString();
1727
}
1828
});
1929

20-
get(new Route("/parameter/:param") {
30+
post(new Route("/add/:item") {
2131
@Override
2232
public Object handle(Request request, Response response) {
23-
StringBuffer myParam = new StringBuffer(request.params(":param"));
24-
return "I reversed your param for ya \"" + myParam.reverse() + "\"";
33+
HelloSpark.things.add(request.params(":item"));
34+
response.status(200);
35+
return response;
2536
}
2637
});
2738
}

0 commit comments

Comments
 (0)
0