Created
November 9, 2013 06:40
-
-
Save taywils/7382471 to your computer and use it in GitHub Desktop.
spark_blog_step_1_2
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 static spark.Spark.*; | |
import spark.Request; | |
import spark.Response; | |
import spark.Route; | |
import java.util.ArrayDeque; | |
import java.util.Deque; | |
public class HelloSpark { | |
public static Deque<Article> articles = new ArrayDeque<Article>(); | |
public static void main(String[] args) { | |
get(new Route("/") { | |
@Override | |
public Object handle(Request request, Response response) { | |
String title = "My Blog"; | |
String createArticleLink = "<a href='/article/create'>Write Article</a>"; | |
StringBuilder html = new StringBuilder(); | |
html.append("<h1>").append(title).append("</h1>").append(createArticleLink); | |
html.append("<hr>"); | |
if(HelloSpark.articles.isEmpty()) { | |
html.append("<b>No articles have been posted</b>"); | |
} else { | |
for(Article article : HelloSpark.articles) { | |
html.append("Title: ").append(article.getTitle()) | |
.append("<br/>") | |
.append(article.getCreatedAt()) | |
.append("<br/>") | |
.append("Summary: ").append(article.getSummary()) | |
.append("<br/>") | |
.append(article.getEditLink()).append(" | ").append(article.getDeleteLink()) | |
.append("</p>"); | |
} | |
} | |
return html.toString(); | |
} | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment